Skip to content

Commit 45f4591

Browse files
author
kaidul
committed
650 problems solved
1 parent 025a546 commit 45f4591

12 files changed

+632
-443
lines changed

Diff for: README.md

+394-391
Large diffs are not rendered by default.

Diff for: source-code/Encode_and_Decode_TinyURL.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
11
class Solution {
2+
public:
3+
unordered_map<string, string> url2Code;
4+
unordered_map<string, string> code2Url;
5+
6+
string alphaNums = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
7+
int ENCODE_LENGTH = 6;
8+
9+
string generateCode(string const& longUrl) {
10+
string code = "";
11+
for (int i = 0; i < ENCODE_LENGTH; i++) {
12+
code += alphaNums[rand() % alphaNums.length()];
13+
}
14+
15+
return code;
16+
}
17+
18+
// Encodes a URL to a shortened URL.
19+
string encode(string longUrl) {
20+
if (url2Code.find(longUrl) != url2Code.end()) {
21+
return "http://tinyurl.com/" + url2Code[longUrl];
22+
}
23+
string code;
24+
do {
25+
code = generateCode(longUrl);
26+
} while (code2Url.find(code) != code2Url.end());
27+
code2Url[code] = longUrl;
28+
url2Code[longUrl] = code;
29+
30+
return "http://tinyurl.com/" + code;
31+
}
32+
33+
// Decodes a shortened URL to its original URL.
34+
string decode(string shortUrl) {
35+
string code = shortUrl.substr(shortUrl.length() - ENCODE_LENGTH, ENCODE_LENGTH);
36+
return code2Url[code];
37+
}
38+
};
39+
40+
41+
// joke
42+
class Solution {
243
public:
344

445
// Encodes a URL to a shortened URL.

Diff for: source-code/Jewels_and_Stones.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int numJewelsInStones(string J, string S) {
4+
vector<bool> isJewel(256, false);
5+
for (char jewel : J) {
6+
isJewel[jewel] = true;
7+
}
8+
int count = 0;
9+
for (char stone : S) {
10+
count += isJewel[stone];
11+
}
12+
13+
return count;
14+
}
15+
};

Diff for: source-code/LRU_Cache.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class LRUCache {
8484
}
8585
};
8686

87-
// using C++ list. got RE for memory corruption though :(
87+
88+
// using C++ std::list
8889
class LRUCache {
8990
public:
9091
LRUCache(int capacity) {
@@ -95,27 +96,24 @@ class LRUCache {
9596

9697
int get(int key) {
9798
if (table.find(key) != table.end()) {
98-
auto node = table[key];
99-
pair<int, int> entry = *node;
100-
entries.erase(node);
101-
entries.push_front(entry);
102-
return entry.second;
99+
auto entry = table[key];
100+
entries.splice(entries.begin(), entries, entry);
101+
return entry->second;
103102
}
104103
return -1;
105104
}
106105

107106
void put(int key, int value) {
108-
pair<int, int> newEntry = {key, value};
109107
if (table.find(key) != table.end()) {
110-
auto curr = table[key];
111-
entries.erase(curr);
108+
entries.erase(table[key]);
112109
} else {
113-
if(entries.size() >= capacity) {
110+
if(entries.size() >= capacity) {
114111
pair<int, int> tailEntry = entries.back();
115-
entries.pop_back();
116112
table.erase(tailEntry.first);
117-
}
113+
entries.pop_back();
114+
}
118115
}
116+
pair<int, int> newEntry = {key, value};
119117
entries.push_front(newEntry);
120118
table[key] = entries.begin();
121119
}

Diff for: source-code/Number_of_Islands_II.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ class Solution {
2727
for(int i = 0; i < positions.size(); ++i) {
2828
int x = positions[i].first;
2929
int y = positions[i].second;
30-
if(find(x, y, parent) == WATER) {
31-
parent[x][y] = pair<int, int>(x, y);
32-
nSet++;
33-
}
30+
parent[x][y] = pair<int, int>(x, y);
31+
nSet++;
32+
3433
for(int k = 0; k < 4; ++k) {
3534
int newX = x + dx[k];
3635
int newY = y + dy[k];

Diff for: source-code/Perfect_Squares.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
// top-down
2+
class Solution {
3+
int numSquares(int n, vector<int>& dp) {
4+
if (n == 0) {
5+
return 0;
6+
}
7+
if (dp[n] != INT_MAX) {
8+
return dp[n];
9+
}
10+
for (int i = 1; i * i <= n; i++) {
11+
int sqr = i * i;
12+
dp[n] = min(dp[n], 1 + numSquares(n - sqr, dp));
13+
}
14+
15+
return dp[n];
16+
}
17+
public:
18+
int numSquares(int n) {
19+
vector<int> dp(n + 1, INT_MAX);
20+
return numSquares(n, dp);
21+
}
22+
};
23+
124
// DP O(n sqrt(n))
225
class Solution {
326
public:

Diff for: source-code/Remove_Duplicate_Letters.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ class Solution {
1212
}
1313
for(int i = 0; i < n; ++i) {
1414
if(!taken[s[i] - 'a']) {
15-
while(!Stack.empty() and Stack.top() > s[i] and freq[Stack.top() - 'a'] > 1) {
15+
while(!Stack.empty() and Stack.top() > s[i] and freq[Stack.top() - 'a'] > 0) {
1616
char rmv = Stack.top();
1717
Stack.pop();
1818
taken[rmv - 'a'] = false;
19-
freq[rmv - 'a']--;
2019
}
2120
Stack.push(s[i]);
2221
taken[s[i] - 'a'] = true;
23-
} else {
24-
freq[s[i] - 'a']--;
2522
}
23+
freq[s[i] - 'a']--;
2624
}
2725
string result = "";
2826
result.reserve(Stack.size());

Diff for: source-code/Robot_Return_to_Origin.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool judgeCircle(string moves) {
4+
int x = 0;
5+
int y = 0;
6+
for (char ch : moves) {
7+
y += (ch == 'U');
8+
y -= (ch == 'D');
9+
x += (ch == 'R');
10+
x -= (ch == 'L');
11+
}
12+
return (x == 0 and y == 0);
13+
}
14+
};

Diff for: source-code/Robot_Room_Cleaner.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* // This is the robot's control interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* class Robot {
5+
* public:
6+
* // Returns true if the cell in front is open and robot moves into the cell.
7+
* // Returns false if the cell in front is blocked and robot stays in the current cell.
8+
* bool move();
9+
*
10+
* // Robot will stay in the same cell after calling turnLeft/turnRight.
11+
* // Each turn will be 90 degrees.
12+
* void turnLeft();
13+
* void turnRight();
14+
*
15+
* // Clean the current cell.
16+
* void clean();
17+
* };
18+
*/
19+
20+
class Solution {
21+
unordered_map<int, unordered_map<int, int>> grid;
22+
int dx[4] = {-1, 0, 1, 0};
23+
int dy[4] = {0, 1, 0, -1};
24+
int x = 0, y = 0;
25+
int dir = 0;
26+
27+
void setback(Robot& robot) {
28+
robot.turnRight();
29+
robot.turnRight();
30+
robot.move();
31+
robot.turnRight();
32+
robot.turnRight();
33+
}
34+
public:
35+
void cleanRoom(Robot& robot) {
36+
if(grid[x][y] == 1) {
37+
return;
38+
}
39+
40+
robot.clean();
41+
grid[x][y] = 1;
42+
43+
for (int i = 0; i < sizeof(dx) / sizeof(dx[0]); i++) {
44+
if (robot.move()) {
45+
x += dx[dir];
46+
y += dy[dir];
47+
48+
cleanRoom(robot);
49+
50+
setback(robot);
51+
x -= dx[dir];
52+
y -= dy[dir];
53+
}
54+
robot.turnRight();
55+
dir = (dir + 1) % 4;
56+
}
57+
}
58+
};

Diff for: source-code/Summary_Ranges.cpp

+14-26
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,24 @@ class Solution {
22
public:
33
vector<string> summaryRanges(vector<int>& nums) {
44
vector<string> result;
5-
string elem = "";
6-
if(nums.size() > 0) {
7-
elem = to_string(nums[0]) + "->";
8-
}
9-
10-
for(int i = 1; i < nums.size(); ++i) {
11-
bool isRange = false;
12-
13-
while(i < nums.size() and (i > 0 and nums[i] == nums[i - 1] + 1)) {
14-
++i;
15-
isRange = true;
5+
int n = (int)nums.size();
6+
for (int left = 0, right = 0; right < n; left++, right++) {
7+
while (right + 1 < n and nums[right + 1] == nums[right] + 1) {
8+
right++;
169
}
17-
if(isRange) {
18-
elem += to_string(nums[i - 1]);
10+
int rangeLength = right - left + 1;
11+
string range;
12+
if (rangeLength == 1) {
13+
range = to_string(nums[left]);
1914
} else {
20-
elem = elem.substr(0, elem.size() - 2);
21-
// elem.erase(elem.size() - 3, 2);
22-
}
23-
result.push_back(elem);
24-
25-
if(i < nums.size()) {
26-
elem = to_string(nums[i]) + "->";
15+
range = to_string(nums[left]);
16+
range += "->";
17+
range += to_string(nums[right]);
2718
}
28-
29-
}
30-
31-
if(elem.size() > 0 and elem[elem.size() - 1] == '>') {
32-
result.push_back(elem.substr(0, elem.size() - 2));
19+
result.push_back(range);
20+
left = right;
3321
}
34-
22+
3523
return result;
3624
}
3725
};

Diff for: source-code/Trapping_Rain_Water_II.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// visualization of this logic: https://www.youtube.com/watch?v=cJayBq38VYw
2+
class Solution {
3+
public:
4+
int trapRainWater(vector<vector<int>>& heightMap) {
5+
int m = heightMap.size();
6+
if (m <= 0) return 0;
7+
int n = heightMap[0].size();
8+
if (n <= 0) return 0;
9+
vector<vector<bool>> visited(m, vector<bool>(n, false));
10+
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap;
11+
for (int i = 0; i < m; i++) {
12+
minHeap.push({heightMap[i][0], i * n});
13+
visited[i][0] = true;
14+
if (n - 1 > 0) {
15+
minHeap.push({heightMap[i][n - 1], i * n + n - 1});
16+
visited[i][n - 1] = true;
17+
}
18+
}
19+
20+
for (int i = 0; i < n; i++) {
21+
minHeap.push({heightMap[0][i], i});
22+
visited[0][i] = true;
23+
if (m - 1 > 0) {
24+
minHeap.push({heightMap[m - 1][i], (m - 1) * n + i});
25+
visited[m - 1][i] = true;
26+
}
27+
}
28+
29+
int totalWater = 0;
30+
int maxHeight = INT_MIN;
31+
32+
int dx[4] = {-1, 0, 1, 0};
33+
int dy[4] = {0, 1, 0, -1};
34+
35+
while (!minHeap.empty()) {
36+
pair<int, int> curr = minHeap.top();
37+
minHeap.pop();
38+
int currHeight = curr.first, x = curr.second / n, y = curr.second % n;
39+
maxHeight = max(maxHeight, currHeight);
40+
for (int i = 0; i < sizeof(dx) / sizeof(dx[0]); i++) {
41+
int neighX = x + dx[i];
42+
int neighY = y + dy[i];
43+
if (neighX < 0 or neighY < 0 or neighX >= m or neighY >= n or visited[neighX][neighY]) {
44+
continue;
45+
}
46+
totalWater += max(maxHeight - heightMap[neighX][neighY], 0);
47+
minHeap.push({heightMap[neighX][neighY], neighX * n + neighY});
48+
visited[neighX][neighY] = true;
49+
}
50+
}
51+
52+
return totalWater;
53+
}
54+
};

Diff for: source-code/Wiggle_Sort.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ class Solution {
99
if(nums[i] > nums[i + 1]) {
1010
swap(nums[i], nums[i + 1]);
1111
}
12-
flag ^= 1;
1312
} else {
1413
if(nums[i] < nums[i + 1]) {
1514
swap(nums[i], nums[i + 1]);
1615
}
17-
flag ^= 1;
1816
}
17+
flag ^= 1;
1918
}
2019
}
2120
};
22-
// with extra space\
23-
/*
21+
22+
// with extra space
2423
class Solution {
2524
public:
2625
void wiggleSort(vector<int>& nums) {
@@ -40,5 +39,4 @@ class Solution {
4039
nums[i] = tmp[k];
4140
}
4241
}
43-
};
44-
*/
42+
};

0 commit comments

Comments
 (0)