Skip to content

Commit f6c92a2

Browse files
author
Chris Wu
committed
no message
1 parent 6ef0afe commit f6c92a2

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

problems/flood-fill.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def floodFill(self, image, sr, sc, newColor):
3+
stack = [(sr, sc)]
4+
originColor = image[sr][sc]
5+
6+
while stack:
7+
i, j = stack.pop()
8+
9+
if image[i][j]==newColor or image[i][j]!=originColor: continue
10+
11+
image[i][j] = newColor
12+
if i+1<len(image): stack.append((i+1, j))
13+
if 0<=i-1: stack.append((i-1, j))
14+
if j+1<len(image[0]): stack.append((i, j+1))
15+
if 0<=j-1: stack.append((i, j-1))
16+
17+
return image

problems/friend-circles.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,38 @@ def union(p1, p2): #[2]
7272

7373
M = [[1,0,0,1],[0,1,1,0],[0,1,1,1],[1,0,1,1]]
7474
s = Solution()
75-
s.findCircleNum(M)
75+
s.findCircleNum(M)
76+
77+
78+
79+
# 2020/8/12
80+
class Solution(object):
81+
def findCircleNum(self, M):
82+
if not M or not M[0]: return 0
83+
count = 0
84+
visited = set()
85+
86+
for student in xrange(len(M)):
87+
if student in visited: continue
88+
89+
count += 1
90+
91+
#dfs
92+
stack = [student]
93+
while stack:
94+
curr_student = stack.pop()
95+
if curr_student in visited: continue
96+
visited.add(curr_student)
97+
stack.extend([class_mate for class_mate, is_friend in enumerate(M[curr_student]) if is_friend])
98+
99+
return count
100+
101+
"""
102+
If you don't know, DFS, please figure it out then come back.
103+
104+
For every `student`, we use DFS to find all of his direct friends. And put them into `visited`.
105+
Every time we perform a DFS means putting an entire friend circle into `visited`.
106+
107+
Time Complexity: O(N).
108+
Space Complexity: O(N).
109+
"""

problems/max-area-of-island.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution(object):
2+
def maxAreaOfIsland(self, grid):
3+
def dfs(i0, j0):
4+
stack = [(i0, j0)]
5+
area = 0
6+
7+
while stack:
8+
i, j = stack.pop()
9+
if grid[i][j]==0 or grid[i][j]==2: continue
10+
grid[i][j] = 2
11+
area += 1
12+
13+
if i+1<M: stack.append((i+1, j))
14+
if 0<=i-1: stack.append((i-1, j))
15+
if j+1<N: stack.append((i, j+1))
16+
if 0<=j-1: stack.append((i, j-1))
17+
18+
return area
19+
20+
if not grid or not grid[0]: return 0
21+
22+
max_area = 0
23+
M = len(grid)
24+
N = len(grid[0])
25+
26+
for i in xrange(M):
27+
for j in xrange(N):
28+
if grid[i][j]==0 or grid[i][j]==2: continue
29+
max_area = max(max_area, dfs(i, j))
30+
31+
return max_area
32+
33+
"""
34+
0 means water.
35+
1 means land.
36+
2 means land we already visited.
37+
38+
We iterate the map using a double for-loop.
39+
When we encounter 0 or 2, we skip.
40+
When we encounter 1, we do a dfs() to mark all the land on the same island as 2. So we don't count it again.
41+
42+
Time: O(N). We iterate all the island once. If visited, we will skip.
43+
Space: O(1).
44+
"""

problems/number-of-islands.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,45 @@ def explore_adjacent(h_root, w_root):
8585
explore_adjacent(h, w)
8686

8787
return count
88+
89+
90+
class Solution(object):
91+
def numIslands(self, grid):
92+
def dfs(i0, j0):
93+
stack = [(i0, j0)]
94+
95+
while stack:
96+
i, j = stack.pop()
97+
if grid[i][j]=="0" or grid[i][j]=="2": continue
98+
grid[i][j] = "2"
99+
100+
if i+1<M: stack.append((i+1, j))
101+
if 0<=i-1: stack.append((i-1, j))
102+
if j+1<N: stack.append((i, j+1))
103+
if 0<=j-1: stack.append((i, j-1))
104+
105+
if not grid or not grid[0]: return 0
106+
107+
count = 0
108+
M = len(grid)
109+
N = len(grid[0])
110+
111+
for i in xrange(M):
112+
for j in xrange(N):
113+
if grid[i][j]=="0" or grid[i][j]=="2": continue
114+
dfs(i, j)
115+
count += 1
116+
return count
117+
118+
"""
119+
0 means water.
120+
1 means land.
121+
2 means land we already visited.
122+
123+
We iterate the map using a double for-loop.
124+
When we encounter 0 or 2, we skip.
125+
When we encounter 1, we do a dfs() to mark all the land on the same island as 2. So we don't count it again.
126+
127+
Time: O(N). We iterate all the island once. If visited, we will skip.
128+
Space: O(1).
129+
"""

0 commit comments

Comments
 (0)