Skip to content

Commit b12eb7a

Browse files
chore: add LeetCode daily solution
1 parent cff85b6 commit b12eb7a

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Count Unguarded Cells in the Grid (Medium)
2+
3+
**Problem ID:** 2257
4+
**Date:** 2025-11-02
5+
**Link:** https://leetcode.com/problems/count-unguarded-cells-in-the-grid/
6+
7+
## Approach
8+
9+
To solve the problem of counting unguarded cells in a grid, we can adopt the following approach:
10+
11+
### Main Idea:
12+
The primary goal is to determine which cells in the grid are guarded by at least one guard and then count the remaining unguarded cells. A guard can see in four cardinal directions (up, down, left, right) until blocked by a wall or another guard.
13+
14+
### Approach:
15+
1. **Grid Representation**:
16+
- We will use a 2D grid to represent the cells, but given the constraints, we can optimize by using a set to track the positions of guards and walls.
17+
18+
2. **Marking Guarded Cells**:
19+
- For each guard, we will check in all four directions (up, down, left, right) to mark the cells that can be seen by the guard:
20+
- Start from the guard's position and move in the direction until we hit a wall or another guard.
21+
- For each cell that is not a wall or guard, mark it as guarded (using a set to avoid duplicates).
22+
23+
3. **Counting Unguarded Cells**:
24+
- After marking all guarded cells, we can calculate the total number of unguarded cells by:
25+
- Starting with the total number of cells in the grid (`m * n`).
26+
- Subtracting the number of cells that are either guarded or occupied by guards or walls.
27+
28+
### Data Structures:
29+
- **Set**: To keep track of the positions of guarded cells. This allows O(1) average time complexity for insertions and checks.
30+
- **List or Array**: To store the positions of guards and walls for easy iteration.
31+
32+
### Complexity:
33+
- **Time Complexity**: O(m * n) in the worst case, as we may need to check each cell in the grid. However, since the total number of guards and walls is limited to `5 * 10^4`, the actual operations performed will be significantly less than this in practice.
34+
- **Space Complexity**: O(m * n) in the worst case for the set storing guarded cells, but it will be limited by the number of guards and walls in practical scenarios.
35+
36+
### Summary:
37+
By efficiently marking the cells that are guarded based on the positions of guards and walls, we can quickly compute the number of unguarded cells in the grid. This approach leverages sets for efficient membership checking and ensures we respect the constraints of the problem.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class Solution {
5+
public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
6+
Set<String> guarded = new HashSet<>();
7+
Set<String> wallsSet = new HashSet<>();
8+
9+
for (int[] wall : walls) {
10+
wallsSet.add(wall[0] + "," + wall[1]);
11+
}
12+
13+
for (int[] guard : guards) {
14+
int r = guard[0], c = guard[1];
15+
guarded.add(r + "," + c);
16+
// Check north
17+
for (int i = r - 1; i >= 0 && !wallsSet.contains((i) + "," + c); i--) {
18+
guarded.add(i + "," + c);
19+
}
20+
// Check south
21+
for (int i = r + 1; i < m && !wallsSet.contains((i) + "," + c); i++) {
22+
guarded.add(i + "," + c);
23+
}
24+
// Check west
25+
for (int i = c - 1; i >= 0 && !wallsSet.contains(r + "," + (i)); i--) {
26+
guarded.add(r + "," + i);
27+
}
28+
// Check east
29+
for (int i = c + 1; i < n && !wallsSet.contains(r + "," + (i)); i++) {
30+
guarded.add(r + "," + i);
31+
}
32+
}
33+
34+
int totalCells = m * n;
35+
int guardedCells = guarded.size();
36+
int occupiedCells = walls.length + guards.length;
37+
38+
return totalCells - guardedCells - occupiedCells;
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var countUnguarded = function(m, n, guards, walls) {
2+
const grid = Array.from({ length: m }, () => Array(n).fill(0));
3+
4+
for (const [r, c] of guards) {
5+
grid[r][c] = 1; // Mark guards
6+
}
7+
8+
for (const [r, c] of walls) {
9+
grid[r][c] = 2; // Mark walls
10+
}
11+
12+
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
13+
14+
for (const [r, c] of guards) {
15+
for (const [dr, dc] of directions) {
16+
let nr = r + dr;
17+
let nc = c + dc;
18+
while (nr >= 0 && nr < m && nc >= 0 && nc < n && grid[nr][nc] === 0) {
19+
grid[nr][nc] = 3; // Mark guarded cells
20+
nr += dr;
21+
nc += dc;
22+
}
23+
}
24+
}
25+
26+
let unguardedCount = 0;
27+
for (let i = 0; i < m; i++) {
28+
for (let j = 0; j < n; j++) {
29+
if (grid[i][j] === 0) {
30+
unguardedCount++;
31+
}
32+
}
33+
}
34+
35+
return unguardedCount;
36+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int:
3+
grid = [[0] * n for _ in range(m)]
4+
5+
for r, c in guards:
6+
grid[r][c] = 1 # Mark guard positions
7+
for r, c in walls:
8+
grid[r][c] = 2 # Mark wall positions
9+
10+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
11+
12+
for r in range(m):
13+
for c in range(n):
14+
if grid[r][c] == 1: # If there's a guard
15+
for dr, dc in directions:
16+
nr, nc = r, c
17+
while 0 <= nr < m and 0 <= nc < n and grid[nr][nc] == 0:
18+
grid[nr][nc] = 3 # Mark as guarded
19+
nr += dr
20+
nc += dc
21+
22+
unguarded_count = sum(row.count(0) for row in grid)
23+
return unguarded_count

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
266266
- 2025-10-30 — [Minimum Number of Increments on Subarrays to Form a Target Array](https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/) (Hard) → `Hard/2025-10-30-1526-Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array`
267267
- 2025-10-31 — [The Two Sneaky Numbers of Digitville](https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/) (Easy) → `Easy/2025-10-31-3289-The-Two-Sneaky-Numbers-of-Digitville`
268268
- 2025-11-01 — [Delete Nodes From Linked List Present in Array](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/) (Medium) → `Medium/2025-11-01-3217-Delete-Nodes-From-Linked-List-Present-in-Array`
269+
- 2025-11-02 — [Count Unguarded Cells in the Grid](https://leetcode.com/problems/count-unguarded-cells-in-the-grid/) (Medium) → `Medium/2025-11-02-2257-Count-Unguarded-Cells-in-the-Grid`

0 commit comments

Comments
 (0)