-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc_0994_rotting_oranges.py
64 lines (50 loc) · 2.06 KB
/
lc_0994_rotting_oranges.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""994. Rotting Oranges
https://leetcode.com/problems/rotting-oranges/
---
Runtime: 44 ms, faster than 96.26% of Python3 online submissions for Rotting Oranges.
Memory Usage: 14.4 MB, less than 39.37% of Python3 online submissions for Rotting Oranges.
"""
import unittest
from itertools import count
from typing import List
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
"""Find rotting oranges using BFS
Additional optimizations:
- We encode board positions using small numbers of the form x + y * R
- Neighbors of a position p are computed as p ± 1, p ± R
- One selects R ≥ N + 2 since a * R - 1 must be distinguishable from b * R + N
"""
R = max(16, len(grid[0]) + 2)
def select(z):
return {
x + y * R
for y, row in enumerate(grid)
for x, v in enumerate(row)
if v == z
}
fresh, rots = select(1), select(2)
# At each step keep the sets of fresh and currently rotting oranges
for step in count():
# If all oranges are rotten, we are done
if not fresh:
return step
# If no oranges can rot, we stop the process as well
if not rots:
return -1
# If we have some fresh and some just rotten oranges, we compute the next rotting batch
rots = fresh.intersection(
n for p in rots for n in (p + 1, p - 1, p + R, p - R)
)
fresh -= rots
# The loop eventually terminates: either the number of elements in fresh goes down,
# or the process is stopped during the next iteration.
class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
self.f = Solution().orangesRotting
def test_examples(self):
assert self.f([[2, 1, 1], [1, 1, 0], [0, 1, 1]]) == 4
assert self.f([[2, 1, 1], [0, 1, 1], [1, 0, 1]]) == -1
assert self.f([[0, 2]]) == 0
if __name__ == "__main__":
unittest.main()