Skip to content

Commit 7633a1e

Browse files
committed
Nov 21
1 parent c2555dc commit 7633a1e

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from itertools import product
2+
from typing import List
3+
4+
5+
class Solution:
6+
def countUnguarded(
7+
self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]
8+
) -> int:
9+
grid = [[0] * n for _ in range(m)]
10+
for x, y in walls:
11+
grid[x][y] = 1
12+
for x, y in guards:
13+
grid[x][y] = 2
14+
15+
for x, y in guards:
16+
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
17+
nx, ny = x + dx, y + dy
18+
while 0 <= nx < m and 0 <= ny < n and grid[nx][ny] <= 0:
19+
grid[nx][ny] = -1
20+
nx += dx
21+
ny += dy
22+
23+
unguarded = 0
24+
for x, y in product(range(m), range(n)):
25+
if grid[x][y] == 0:
26+
unguarded += 1
27+
return unguarded
28+
29+
30+
def main():
31+
m = 4
32+
n = 6
33+
guards = [[0, 0], [1, 1], [2, 3]]
34+
walls = [[0, 1], [2, 2], [1, 4]]
35+
assert Solution().countUnguarded(m, n, guards, walls) == 7
36+
37+
m = 3
38+
n = 3
39+
guards = [[1, 1]]
40+
walls = [[0, 1], [1, 0], [2, 1], [1, 2]]
41+
assert Solution().countUnguarded(m, n, guards, walls) == 4
42+
43+
44+
if __name__ == '__main__':
45+
main()

2024-11-November-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
| November 18 | [1652. Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/) | Easy | Solved |
2424
| November 19 | [2461. Maximum Sum of Distinct Subarrays With Length K](https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/) | Medium | Solved |
2525
| November 20 | [2516. Take K of Each Character From Left and Right](https://leetcode.com/problems/take-k-of-each-character-from-left-and-right/) | Medium | Solved |
26-
| November 21 | []() | | |
26+
| November 21 | [2257. Count Unguarded Cells in the Grid](https://leetcode.com/problems/count-unguarded-cells-in-the-grid/) | Medium | Solved |
2727
| November 22 | []() | | |
2828
| November 23 | []() | | |
2929
| November 24 | []() | | |
@@ -38,5 +38,5 @@
3838
| Level | Problems | Solved | Unsolved |
3939
| --- | --- | --- | --- |
4040
| Easy | 4 | 4 | 0 |
41-
| Medium | 15 | 8 | 7 |
41+
| Medium | 16 | 9 | 7 |
4242
| Hard | 1 | 1 | 0 |

0 commit comments

Comments
 (0)