Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aad0873

Browse files
2257: Count Unguarded Cells in the Grid
1 parent 53d56d3 commit aad0873

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ My solutions to LeetCode problems in Kotlin.
2929
| [1975](https://leetcode.com/problems/maximum-matrix-sum/) | [Maximum Matrix Sum](src/main/kotlin/com/schmoczer/leetcode/_1975/MaximumMatrixSum.kt) | Medium |
3030
| [2064](https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/) | [Minimized Maximum of Products Distributed to Any Store](src/main/kotlin/com/schmoczer/leetcode/_2064/MinimizedMaximum.kt) | Medium |
3131
| [2070](https://leetcode.com/problems/most-beautiful-item-for-each-query/) | [Most Beautiful Item for Each Query](src/main/kotlin/com/schmoczer/leetcode/_2070/MostBeautifulItemForEachQuery.kt) | Medium |
32+
| [2257](https://leetcode.com/problems/count-unguarded-cells-in-the-grid/) | [Count Unguarded Cells in the Grid](src/main/kotlin/com/schmoczer/leetcode/_2257/CountUnguardedCellsInTheGrid.kt) | Medium |
3233
| [2275](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | [Largest Combination With Bitwise AND Greater Than Zero](src/main/kotlin/com/schmoczer/leetcode/_2275/LargestCombination.kt) | Medium |
3334
| [2490](https://leetcode.com/problems/circular-sentence/) | [Circular Sentence](src/main/kotlin/com/schmoczer/leetcode/_2490/CircularSentence.kt) | Easy |
3435
| [2516](https://leetcode.com/problems/take-k-of-each-character-from-left-and-right/) | [Take K of Each Character From Left and Right](src/main/kotlin/com/schmoczer/leetcode/_2516/TakeKOfEachCharacterFromLeftAndRight.kt) | Medium |
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.schmoczer.leetcode._2257
2+
3+
import com.schmoczer.leetcode._2257.CountUnguardedCellsInTheGrid.Companion.GUARD
4+
import com.schmoczer.leetcode._2257.CountUnguardedCellsInTheGrid.Companion.GUARDED
5+
import com.schmoczer.leetcode._2257.CountUnguardedCellsInTheGrid.Companion.WALL
6+
7+
class CountUnguardedCellsInTheGrid {
8+
internal companion object {
9+
const val UNGUARDED = 0
10+
const val GUARDED = 1
11+
const val GUARD = 2
12+
const val WALL = 3
13+
}
14+
15+
fun countUnguarded(m: Int, n: Int, guards: Array<IntArray>, walls: Array<IntArray>): Int {
16+
var grid = Array<IntArray>(m) { IntArray(n) { UNGUARDED } }
17+
for (guard in guards) {
18+
grid[guard.first()][guard.last()] = GUARD
19+
}
20+
for (wall in walls) {
21+
grid[wall.first()][wall.last()] = WALL
22+
}
23+
for (i in 0 until m) {
24+
var isGuardActive = grid[i][0] == GUARD
25+
for (j in 1 until n) {
26+
isGuardActive = grid.update(i, j, isGuardActive)
27+
}
28+
isGuardActive = grid[i][n - 1] == GUARD
29+
for (j in n - 2 downTo 0) {
30+
isGuardActive = grid.update(i, j, isGuardActive)
31+
}
32+
}
33+
for (j in 0 until n) {
34+
var isGuardActive = grid[0][j] == GUARD
35+
for (i in 1 until m) {
36+
isGuardActive = grid.update(i, j, isGuardActive)
37+
}
38+
isGuardActive = grid[m - 1][j] == GUARD
39+
for (i in m - 2 downTo 0) {
40+
isGuardActive = grid.update(i, j, isGuardActive)
41+
}
42+
}
43+
return grid.sumOf { it.count { it == UNGUARDED } }
44+
}
45+
}
46+
47+
private fun Array<IntArray>.update(i: Int, j: Int, isGuardActive: Boolean): Boolean {
48+
if (this[i][j] == GUARD) {
49+
return true
50+
}
51+
if (this[i][j] == WALL) {
52+
return false
53+
}
54+
if (isGuardActive) {
55+
this[i][j] = GUARDED
56+
}
57+
return isGuardActive
58+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Count Unguarded Cells in the Grid
2+
3+
You are given two integers `m` and `n` representing a 0-indexed `m x n` grid. You are also given two 2D integer arrays
4+
`guards` and `walls` where `guards[i] = [rowi, coli]` and `walls[j] = [rowj, colj]` represent the positions of the `i`'
5+
th guard and `j`'th wall respectively.
6+
7+
A guard can see every cell in the four cardinal directions (north, east, south, or west) starting from their position
8+
unless obstructed by a wall or another guard. A cell is guarded if there is at least one guard that can see it.
9+
10+
Return the number of unoccupied cells that are not guarded.
11+
12+
Example 1:
13+
14+
> Input: m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
15+
>
16+
> Output: 7
17+
18+
Example 2:
19+
20+
> Input: m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
21+
>
22+
> Output: 4
23+
24+
Constraints:
25+
26+
- `1 <= m, n <= 10^5`
27+
- `2 <= m * n <= 10^5`
28+
- `1 <= guards.length, walls.length <= 5 * 10^4`
29+
- `2 <= guards.length + walls.length <= m * n`
30+
- `guards[i].length == walls[j].length == 2`
31+
- `0 <= rowi, rowj < m`
32+
- `0 <= coli, colj < n`
33+
- All the positions in `guards` and `walls` are unique.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.schmoczer.leetcode._2257
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
class CountUnguardedCellsInTheGridTest {
8+
private lateinit var sut: CountUnguardedCellsInTheGrid
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = CountUnguardedCellsInTheGrid()
13+
}
14+
15+
@Test
16+
fun `7 unguarded cells in a 4x6 grid`() {
17+
val m = 4
18+
val n = 6
19+
val guards = arrayOf(intArrayOf(0, 0), intArrayOf(1, 1), intArrayOf(2, 3))
20+
val walls = arrayOf(intArrayOf(0, 1), intArrayOf(2, 2), intArrayOf(1, 4))
21+
val expected = 7
22+
23+
val result = sut.countUnguarded(m, n, guards, walls)
24+
25+
assertEquals(expected, result)
26+
}
27+
28+
@Test
29+
fun `4 unguarded cells in a 3x3 grid`() {
30+
val m = 3
31+
val n = 3
32+
val guards = arrayOf(intArrayOf(1, 1))
33+
val walls = arrayOf(intArrayOf(0, 1), intArrayOf(1, 0), intArrayOf(2, 1), intArrayOf(1, 2))
34+
val expected = 4
35+
36+
val result = sut.countUnguarded(m, n, guards, walls)
37+
38+
assertEquals(expected, result)
39+
}
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /