Skip to content

Commit

Permalink
Add LC 130
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyannn committed Jan 18, 2024
1 parent 3efe906 commit 8aed46e
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions lc_0130_surrounding_regions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import unittest
from typing import List


class Solution:
def solve(self, grid: List[List[str]]) -> None:
m, n = len(grid[0]), len(grid)
zeros = {
(x, y)
for y, row in enumerate(grid)
for x, ch in enumerate(row)
if ch == "O"
}
saved = {(x, y) for x, y in zeros if not x or not y or x == m - 1 or y == n - 1}

def neighbors(x, y):
yield x + 1, y
yield x - 1, y
yield x, y + 1
yield x, y - 1

unsaved = zeros - saved

q = list(saved)
while q:
p = q.pop()
for n in neighbors(*p):
if n in unsaved:
unsaved.remove(n)
q.append(n)

for x, y in unsaved:
grid[y][x] = "X"


class TestSolution(unittest.TestCase):
def test_solve(self):
s = Solution()

grid = [
["X", "X", "X", "X"],
["X", "O", "O", "X"],
["X", "X", "O", "X"],
["X", "O", "X", "X"],
]
s.solve(grid)

expected = [
["X", "X", "X", "X"],
["X", "X", "X", "X"],
["X", "X", "X", "X"],
["X", "O", "X", "X"],
]

self.assertEqual(grid, expected)


if __name__ == "__main__":
unittest.main()

0 comments on commit 8aed46e

Please sign in to comment.