Skip to content

Commit c303003

Browse files
author
Your Name
committed
day 10
1 parent 69f9f26 commit c303003

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

130SurroundedRegions.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Given an m x n matrix board containing 'X' and 'O', capture all regions that are 4-directionally surrounded by 'X'.
2+
3+
# A region is captured by flipping all 'O's into 'X's in that surrounded region.
4+
5+
# [["X","X","X","X"],
6+
# ["X","O","O","X"],
7+
# ["X","X","O","X"],
8+
# ["X","O","X","X"]]
9+
10+
11+
# [["X","X","X","X"],
12+
# ["X","X","X","X"],
13+
# ["X","X","X","X"],
14+
# ["X","O","X","X"]]
15+
16+
17+
[["X","X","X"],
18+
["X","O","X"],
19+
["X","X","X"]]
20+
21+
class Solution(object):
22+
def solve(self, board):
23+
"""
24+
:type board: List[List[str]]
25+
:rtype: None Do not return anything, modify board in-place instead.
26+
"""
27+
visited = set()
28+
movement = [(0, -1), (0, 1), (1, 0), (-1, 0)]
29+
rowSize = len(board)
30+
colSize = len(board[0])
31+
def helper(startx, starty):
32+
queue = []
33+
currentVisited = set()
34+
queue.append((startx, starty))
35+
currentVisited.add((startx, starty))
36+
flag = True
37+
while queue:
38+
curr = queue.pop(0)
39+
for step in movement:
40+
newx = curr[0] + step[0]
41+
newy = curr[1] + step[1]
42+
if newx == -1 or newx == rowSize or newy == -1 or newy == colSize:
43+
flag = False
44+
else:
45+
if board[newx][newy] == 'O' and (newx, newy) not in currentVisited:
46+
currentVisited.add((newx, newy))
47+
visited.add((newx, newy))
48+
queue.append((newx, newy))
49+
if flag:
50+
# surrounded by 'o'
51+
for cell in currentVisited:
52+
board[cell[0]][cell[1]] = 'X'
53+
54+
for i in range(rowSize):
55+
for j in range(colSize):
56+
if (i,j) not in visited and board[i][j] == 'O':
57+
helper(i, j)
58+
return board
59+
60+
sol = Solution()
61+
res = sol.solve([["X","X","X"],
62+
["X","O","X"],
63+
["X","X","X"]])
64+
print(res)

150EvaluateReversePolishNotation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ def evalRPN(self, tokens):
2727
return stack[0]
2828

2929
sol = Solution()
30-
res = sol.evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
31-
)
30+
res = sol.evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"])
3231
print(res)

200NumberofIslands.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
2+
3+
# An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
4+
5+
6+
7+
# Example 1:
8+
9+
# Input: grid = [
10+
# ["1","1","1","1","0"],
11+
# ["1","1","0","1","0"],
12+
# ["1","1","0","0","0"],
13+
# ["0","0","0","0","0"]
14+
# ]
15+
# Output: 1
16+
17+
class Solution(object):
18+
def numIslands(self, grid):
19+
"""
20+
:type grid: List[List[str]]
21+
:rtype: int
22+
"""
23+
rowSize = len(grid)
24+
colSize = len(grid[0])
25+
move = [(0, -1), (0, 1), (-1, 0), (1, 0)]
26+
visited = set()
27+
count = 0
28+
def helper(startx, starty):
29+
# up y--
30+
# down y++
31+
# left x--
32+
# right x++
33+
queue = []
34+
queue.append((startx, starty))
35+
visited.add((startx, starty))
36+
while queue:
37+
curr = queue.pop(0)
38+
currx = curr[0]
39+
curry = curr[1]
40+
for step in move:
41+
newx = currx + step[0]
42+
newy = curry + step[1]
43+
if (newx, newy) not in visited and newx > -1 and newx < rowSize and newy > -1 and newy < colSize and grid[newx][newy] == '1':
44+
queue.append((newx, newy))
45+
visited.add((newx, newy))
46+
for i in range(rowSize):
47+
for j in range(colSize):
48+
if grid[i][j] == '1' and (i, j) not in visited:
49+
helper(i, j)
50+
count += 1
51+
return count
52+
sol = Solution()
53+
res = sol.numIslands(grid = [
54+
["1","1","1","1","0"],
55+
["1","1","0","1","0"],
56+
["1","1","0","0","0"],
57+
["0","0","0","0","0"]
58+
])
59+
print(res)
60+

0 commit comments

Comments
 (0)