Skip to content

Commit b0c1249

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

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

problems/flood-fill.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ def floodFill(self, image, sr, sc, newColor):
1414
if j+1<len(image[0]): stack.append((i, j+1))
1515
if 0<=j-1: stack.append((i, j-1))
1616

17-
return image
17+
return image
18+
19+
"""
20+
DFS the `image`.
21+
If the color is newColor, we don't need to see.
22+
If the color is not originalColor, we don't need to see, too.
23+
Else we paint the node to newColor.
24+
25+
Time: O(N).
26+
Space: O(1).
27+
"""

problems/keys-and-rooms.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def canVisitAllRooms(self, rooms):
3+
if not rooms: return True
4+
5+
visited = set()
6+
stack = [0]
7+
8+
while stack:
9+
key = stack.pop()
10+
if key in visited: continue
11+
visited.add(key)
12+
stack.extend(rooms[key])
13+
14+
return len(rooms)==len(visited)
15+
16+
"""
17+
Time: O(N).
18+
Space: O(N).
19+
N is the number of rooms.
20+
"""

0 commit comments

Comments
 (0)