Skip to content

Commit 0931d9e

Browse files
committed
200_Number_of_Islands
305_Number_of_Islands_II
1 parent 1d96962 commit 0931d9e

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
| 154 | [Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/154_Find_Minimum_in_Rotated_Sorted_Array_II.py) | Binary search with conditions, A[l] > A[r], A[l]=A[mid]=A[r] |
6060
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/155_Min_Stack.py) | Add another stack for min stack, maintance this stack when the main stack pop or push |
6161
| 156 | [Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/) ♥| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/156_Binary_Tree_Upside_Down.py) | p.left = parent.right, parent.right = p.right, p.right = parent, parent = p.left, p = left |
62-
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/157_Read_N_Characters_Given_Read4.py) | Handle the edge case (the end) |
62+
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/157_Read_N_Characters_Given_Read4.py) | Handle the edge case (the end) |
6363
| 158 | [Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) ♥| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/158_Read_N_Characters_Given_Read4_II_Call_multiple_times.py) | Store the pos and offset that is read by last read4 |
6464
| 159 | [Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/159_Longest_Substring_with_At_Most_Two_Distinct_Characters.py) | Maintain a sliding window that always satisfies such condition |
6565
| 161 | [One Edit Distance](https://leetcode.com/problems/one-edit-distance/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/161_One_Edit_Distance.py) | 1. Check the different position and conditions<br>2. [Edit distance](https://leetcode.com/problems/edit-distance/)|
@@ -68,6 +68,7 @@
6868
| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/167_Two_Sum_II_Input_array_is_sorted.py) | Two points O(n) and O(1) |
6969
| 186 | [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/186_Reverse_Words_in_a_String_II.py) | Reverse all and reverse each words |
7070
| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/198_House_Robber.py) | f(k) = max(f(k – 2) + num[k], f(k – 1)), O(n) and O(1) |
71+
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/200_Number_of_Islands.py) | 1. Quick union find, O(nlogn) and O(n^2)<br>2. BFS with marks, O(n^2) and O(1) |
7172
| 213 | [House Robber II](https://leetcode.com/problems/house-robber-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/213_House_Robber_II.py) | f(k) = max(f(k – 2) + num[k], max(dp[0~ls-2],dp[1~ls-1], O(n) and O(1)|
7273
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/217_Contains_Duplicate.py) | 1. Set and compare length<br>2. Sort and check i,i +1|
7374
| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/219_Contains_Duplicate_II.py) | 1. Brute force<br> 2. Maintenance a set that contains previous k numbers, and check if curr in set |
@@ -78,13 +79,15 @@
7879
| 252 | [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/252_Meeting_Rooms.py) | 1. Sort and compare intervals[i].end with intervals[i+1], O(nlogn) and O(1)<br>2. Sort and check intervals when count >= 2, O(nlogn) and O(n) |
7980
| 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/259_3Sum_Smaller.py) | 1. Reduce to two sum smaller, then binary search, O(n^2lgn) and O(1)<br>2. Reduce to two sum smaller, then two points, O(n^2) and O(1)|
8081
| 288 | [Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/288_Unique_Word_Abbreviation.py) | hash |
82+
| 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/305_Number_of_Islands_II.py) | Quick union find with weights, O(nlogn) and O(n) |
8183
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/337_House_Robber_III.py) | 1. Recursion with hash map, O(n) and O(n)<br>2. Recursion on two steps, O(n) and O(1) |
8284
| 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/339_Nested_List_Weight_Sum.py) | Depth-first recursion |
8385
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|
8486
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/351_Android_Unlock_Patterns.py) | |
8587

8688

87-
| # | ToDo |
89+
| # | To Understand |
8890
|---| ----- |
89-
91+
| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) |
9092
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) &hearts; |
93+

python/200_Number_of_Islands.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def numIslands(self, grid):
3+
"""
4+
:type grid: List[List[str]]
5+
:rtype: int
6+
"""
7+
# BFS with marks
8+
if grid is None or len(grid) == 0:
9+
return 0
10+
islands = 0
11+
for i in range(len(grid)):
12+
for j in range(len(grid[i])):
13+
if grid[i][j] == '1':
14+
self.explore(grid, i, j)
15+
islands += 1
16+
return islands
17+
18+
def explore(self, grid, i, j):
19+
grid[i][j] = 'X'
20+
if i - 1 >= 0 and grid[i - 1][j] == '1':
21+
self.explore(grid, i - 1, j)
22+
if j - 1 >= 0 and grid[i][j - 1] == '1':
23+
self.explore(grid, i, j - 1)
24+
if i + 1 < len(grid) and grid[i + 1][j] == '1':
25+
self.explore(grid, i + 1, j)
26+
if j + 1 < len(grid[i]) and grid[i][j + 1] == '1':
27+
self.explore(grid, i, j + 1)

python/305_Number_of_Islands_II.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution(object):
2+
def numIslands2(self, m, n, positions):
3+
"""
4+
:type m: int
5+
:type n: int
6+
:type positions: List[List[int]]
7+
:rtype: List[int]
8+
"""
9+
# quick union find with weights
10+
ans = []
11+
islands = Union()
12+
for p in map(tuple, positions):
13+
islands.add(p)
14+
for dp in (0, 1), (0, -1), (1, 0), (-1, 0):
15+
q = (p[0] + dp[0], p[1] + dp[1])
16+
if q in islands.id:
17+
islands.unite(p, q)
18+
ans += [islands.count]
19+
return ans
20+
21+
class Union(object):
22+
"""
23+
quick union find with weights
24+
"""
25+
def __init__(self):
26+
# use dic to reduce index operations
27+
self.id = {}
28+
self.sz = {}
29+
self.count = 0
30+
31+
def add(self, p):
32+
self.id[p] = p
33+
self.sz[p] = 1
34+
self.count += 1
35+
36+
def root(self, i):
37+
while i != self.id[i]:
38+
self.id[i] = self.id[self.id[i]]
39+
i = self.id[i]
40+
return i
41+
42+
def unite(self, p, q):
43+
i, j = self.root(p), self.root(q)
44+
if i == j:
45+
return
46+
if self.sz[i] > self.sz[j]:
47+
i, j = j, i
48+
self.id[i] = j
49+
self.sz[j] += self.sz[i]
50+
self.count -= 1

0 commit comments

Comments
 (0)