Skip to content

Commit d58b053

Browse files
committed
695_Max_Area_of_Island
1 parent 63e4d70 commit d58b053

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ Also, there are open source implementations for basic data structs and algorithm
153153
| 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/482_License_Key_Formatting.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/482_License_Key_Formatting.java) | String processing, lower and len % K, O(n) and O(n) |
154154
| 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/538_Convert_BST_to_Greater_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/538_Convert_BST_to_Greater_Tree.java) | Right first DFS with a variable recording sum of node.val and right.val. 1. Recursive.<br>2. Stack 3. Reverse Morris In-order Traversal |
155155
| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/543_Diameter_of_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/543_Diameter_of_Binary_Tree.java) | DFS with O(1) for max answer |
156+
| 547 | [Friend Circles](https://leetcode.com/problems/friend-circles/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/547_Friend_Circles.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/547_Friend_Circles.java) | 1. DFS, O(n^2) and O(1)<br>2. BFS, O(n^2) and O(1)<br>3. Union-find, |
156157
| 557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/557_Reverse_Words_in_a_String_III.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/557_Reverse_Words_in_a_String_III.java) | String handle: Split with space than reverse word, O(n) and O(n). Better solution is that reverse can be O(1) space in array. |
157158
| 560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/560_Subarray_Sum_Equals_K.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/560_Subarray_Sum_Equals_K.java) | Note that there are n^2 possible pairs, so the key point is accelerate computation for sum and reduce unnecessary pair. 1. Cummulative sum, O(n^2) and O(1)/O(n)<br>2. Add sum into hash, check if sum - k is in hash, O(n) and O(n) |
158159
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/572_Subtree_of_Another_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/572_Subtree_of_Another_Tree.java) | 1. Tree traverse and compare<br>2. Tree to string and compare |
@@ -163,6 +164,7 @@ Also, there are open source implementations for basic data structs and algorithm
163164
| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) |
164165
| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/680_Valid_Palindrome_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/680_Valid_Palindrome_II.java) | Recursively check s[left == end, when not equal delete left or right. |
165166
| 692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/692_Top_K_Frequent_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/692_Top_K_Frequent_Words.java) | 1. Sort based on frequency and alphabetical order, O(nlgn) and O(n)<br>2. Find top k with Heap, O(nlogk) and O(n) |
167+
| 695 | [Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/695_Max_Area_of_Island.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/695_Max_Area_of_Island.java) | 1. DFS, O(n^2) and O(n)<br>2. BFS, O(n^2) and O(n)|
166168
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/697_Degree_of_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/697_Degree_of_an_Array.java) | 1. Find degree and value, then find smallest subarray (start and end with this value), O(n) and O(n)<br>2. Go through nums, remember left most pos and right most for each value, O(n) and O(n) |
167169
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/703_Kth_Largest_Element_in_a_Stream.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/703_Kth_Largest_Element_in_a_Stream.java) | 1. Sort and insert into right place, O(nlgn) and O(n)<br>2. k largest heap, O(nlogk) and O(n) |
168170
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/706_Design_HashMap.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/706_Design_HashMap.java) | Hash implementation, mod is fine. Be careful about key conflict and key remove. |

java/695_Max_Area_of_Island.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*class Solution {
2+
int[][] grid;
3+
4+
public int area(int r, int c) {
5+
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || grid[r][c] == 0)
6+
return 0;
7+
grid[r][c] = 0;
8+
return (1 + area(r + 1, c) + area(r - 1, c)
9+
+ area(r, c - 1) + area(r, c + 1));
10+
}
11+
12+
public int maxAreaOfIsland(int[][] grid) {
13+
this.grid = grid;
14+
int ans = 0;
15+
for (int r = 0; r < grid.length; r++) {
16+
for (int c = 0; c < grid[0].length; c++) {
17+
ans = Math.max(ans, area(r, c));
18+
}
19+
}
20+
return ans;
21+
}
22+
}*/
23+
class Solution {
24+
// DFS and you can implement BFS with queue
25+
public int maxAreaOfIsland(int[][] grid) {
26+
int[] dr = new int[]{1, -1, 0, 0};
27+
int[] dc = new int[]{0, 0, 1, -1};
28+
int ans = 0;
29+
for (int r0 = 0; r0 < grid.length; r0++) {
30+
for (int c0 = 0; c0 < grid[0].length; c0++) {
31+
if (grid[r0][c0] == 1) {
32+
int shape = 0;
33+
Stack<int[]> stack = new Stack();
34+
stack.push(new int[]{r0, c0});
35+
grid[r0][c0] = 0;
36+
while (!stack.empty()) {
37+
int[] node = stack.pop();
38+
int r = node[0], c = node[1];
39+
shape++;
40+
for (int k = 0; k < 4; k++) {
41+
int nr = r + dr[k];
42+
int nc = c + dc[k];
43+
if (0 <= nr && nr < grid.length &&
44+
0 <= nc && nc < grid[0].length &&
45+
grid[nr][nc] == 1) {
46+
stack.push(new int[]{nr, nc});
47+
grid[nr][nc] = 0;
48+
}
49+
}
50+
}
51+
ans = Math.max(ans, shape);
52+
}
53+
}
54+
}
55+
return ans;
56+
}
57+
}

python/695_Max_Area_of_Island.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class Solution(object):
2+
def maxAreaOfIsland(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
# because
8+
ans = 0
9+
for i in range(len(grid)):
10+
for j in range(len(grid[0])):
11+
if grid[i][j] == 1:
12+
grid[i][j] = 0
13+
ans = max(self.dfs(grid, i, j), ans)
14+
# ans = max(self.bfs(grid, i, j), ans)
15+
return ans
16+
17+
def dfs(self, grid, i, j):
18+
# DFS based on stack
19+
stack = [(i, j)]
20+
area = 0
21+
# Stack for DFS
22+
while stack:
23+
r, c = stack.pop(-1)
24+
area += 1
25+
for nr, nc in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):
26+
if (0 <= nr < len(grid) and
27+
0 <= nc < len(grid[0]) and grid[nr][nc]):
28+
stack.append((nr, nc))
29+
grid[nr][nc] = 0
30+
return area
31+
32+
# def bfs(self, grid, x, y):
33+
# # BFS based on queue
34+
# queue = [(x, y)]
35+
# area = 0
36+
# # Stack for DFS
37+
# while queue:
38+
# i, j = queue.pop(0)
39+
# area += 1
40+
# if i - 1 >= 0 and grid[i - 1][j] == 1:
41+
# grid[i - 1][j] = 0
42+
# queue.append((i - 1, j))
43+
# if i + 1 < len(grid) and grid[i + 1][j] == 1:
44+
# grid[i + 1][j] = 0
45+
# queue.append((i + 1, j))
46+
# if j - 1 >= 0 and grid[i][j - 1] == 1:
47+
# grid[i][j - 1] = 0
48+
# queue.append((i, j - 1))
49+
# if j + 1 < len(grid[0]) and grid[i][j + 1] == 1:
50+
# grid[i][j + 1] = 0
51+
# queue.append((i, j + 1))
52+
# return area
53+
54+
# def maxAreaOfIsland(self, grid):
55+
# # Recursive DFS
56+
# seen = set()
57+
# def area(r, c):
58+
# if not (0 <= r < len(grid) and 0 <= c < len(grid[0])
59+
# and (r, c) not in seen and grid[r][c]):
60+
# return 0
61+
# seen.add((r, c))
62+
# return (1 + area(r+1, c) + area(r-1, c) +
63+
# area(r, c-1) + area(r, c+1))
64+
65+
# return max(area(r, c)
66+
# for r in range(len(grid))
67+
# for c in range(len(grid[0])))

0 commit comments

Comments
 (0)