LeetCode in Kotlin

2290. Minimum Obstacle Removal to Reach Corner

Hard

You are given a 0-indexed 2D integer array grid of size m x n. Each cell has one of two values:

You can move up, down, left, or right from and to an empty cell.

Return the minimum number of obstacles to remove so you can move from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1).

Example 1:

Input: grid = [[0,1,1],[1,1,0],[1,1,0]]

Output: 2

Explanation: We can remove the obstacles at (0, 1) and (0, 2) to create a path from (0, 0) to (2, 2).

It can be shown that we need to remove at least 2 obstacles, so we return 2.

Note that there may be other ways to remove 2 obstacles to create a path.

Example 2:

Input: grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]

Output: 0

Explanation: We can move from (0, 0) to (2, 4) without removing any obstacles, so we return 0.

Constraints:

Solution

import java.util.PriorityQueue
import java.util.Queue

class Solution {
    fun minimumObstacles(grid: Array<IntArray>): Int {
        val n = grid.size
        val m = grid[0].size
        val dirs = arrayOf(intArrayOf(0, 1), intArrayOf(1, 0), intArrayOf(0, -1), intArrayOf(-1, 0))
        val q: Queue<State> = PriorityQueue { a: State, b: State -> a.removed - b.removed }
        q.add(State(0, 0, 0))
        val visited = Array(n) { BooleanArray(m) }
        visited[0][0] = true
        while (q.isNotEmpty()) {
            val state = q.poll()
            if (state.r == n - 1 && state.c == m - 1) {
                return state.removed
            }
            for (d in dirs) {
                val nr = state.r + d[0]
                val nc = state.c + d[1]
                if (nr < 0 || nc < 0 || nr == n || nc == m || visited[nr][nc]) {
                    continue
                }
                visited[nr][nc] = true
                val next = State(nr, nc, state.removed)
                if (grid[nr][nc] == 1) {
                    next.removed++
                }
                q.add(next)
            }
        }
        return -1
    }

    private class State(var r: Int, var c: Int, var removed: Int)
}