Skip to content

Commit 02f9078

Browse files
committed
Dec18
1 parent 41e4ca0 commit 02f9078

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ Solutions <a href = "https://github.com/aliasvishnu/leetcode/tree/master/Solutio
8080
| 119 | BF | Generate new row from old row. | O(n^2) |
8181
| 128 | DP | DP problem, find recurrence relation. | |
8282
| 413 | Array | [1, 2, 3, 4, 5, 7, 9, 11]. can be written as [5, 3] i.e. 5 sequence of difference 1 and 3 sequence of difference 2, you need to figure out how many parts you can split 5 and 3 into. | O(n) |
83-
| 738 | Array | Find the first time Xi > Xi+1, Xi -= 1 and turn all Xi+k = 9, For eg, 321 becomes 299. However if there are repeating digits like 33332, then subtract the first occurrence then rest become 9, 33332 becomes 29999. | O(n) |
83+
| 694 | DFS | Keep track of the directions in which DFS proceeds in some form, maybe a string like ddr for down down right. | O(rows*cols) |
84+
| 738 | Array | Find the first time Xi > Xi+1, Xi -= 1 and turn all Xi+k = 9, For eg, 321 becomes 299. Figure out cases like 33332. | O(n) |
8485

8586
## Second hint list
8687

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
unordered_set<string> st;
4+
int rows, cols, count = 0;
5+
6+
void DFS(vector<vector<int>> &grid, int r, int c, string &s, string code){
7+
if(r < 0 || r >= rows) { s += ","; return;}
8+
if(c < 0 || c >= cols) { s += ","; return;}
9+
if(grid[r][c] == 0) { s += ","; return;}
10+
else{
11+
grid[r][c] = 0;
12+
s += code;
13+
}
14+
15+
DFS(grid, r+1, c, s, "d");
16+
DFS(grid, r, c+1, s, "r");
17+
DFS(grid, r-1, c, s, "l");
18+
DFS(grid, r, c-1, s, "u");
19+
}
20+
21+
int numDistinctIslands(vector<vector<int>>& grid) {
22+
rows = grid.size();
23+
if(rows == 0) return 0;
24+
cols = grid[0].size();
25+
if(cols == 0) return 0;
26+
27+
for(int i = 0; i < rows; i++){
28+
for(int j = 0; j < cols; j++){
29+
if(grid[i][j] == 1){
30+
string order = "";
31+
DFS(grid, i, j, order, "s");
32+
if(st.find(order) == st.end()){
33+
count += 1;
34+
st.insert(order);
35+
// cout << order << endl;
36+
}
37+
}
38+
}
39+
}
40+
41+
return count;
42+
}
43+
};

0 commit comments

Comments
 (0)