All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1201_1300.s1219_path_with_maximum_gold.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g1201_1300.s1219_path_with_maximum_gold

// #Medium #Array #Matrix #Backtracking #2023_06_09_Time_238_ms_(100.00%)_Space_34.3_MB_(100.00%)

class Solution {
    private var maxGold = 0
    fun getMaximumGold(grid: Array): Int {
        for (i in grid.indices) {
            for (j in grid[0].indices) {
                if (grid[i][j] != 0) {
                    val g = grid[i][j]
                    grid[i][j] = 0
                    gold(grid, i, j, g)
                    grid[i][j] = g
                }
            }
        }
        return maxGold
    }

    private fun gold(grid: Array, row: Int, col: Int, gold: Int) {
        if (gold > maxGold) {
            maxGold = gold
        }
        if (row > 0 && grid[row - 1][col] != 0) {
            val currGold = grid[row - 1][col]
            grid[row - 1][col] = 0
            gold(grid, row - 1, col, gold + currGold)
            grid[row - 1][col] = currGold
        }
        if (col > 0 && grid[row][col - 1] != 0) {
            val currGold = grid[row][col - 1]
            grid[row][col - 1] = 0
            gold(grid, row, col - 1, gold + currGold)
            grid[row][col - 1] = currGold
        }
        if (row < grid.size - 1 && grid[row + 1][col] != 0) {
            // flag=false;
            val currGold = grid[row + 1][col]
            grid[row + 1][col] = 0
            gold(grid, row + 1, col, gold + currGold)
            grid[row + 1][col] = currGold
        }
        if (col < grid[0].size - 1 && grid[row][col + 1] != 0) {
            val currGold = grid[row][col + 1]
            grid[row][col + 1] = 0
            gold(grid, row, col + 1, gold + currGold)
            grid[row][col + 1] = currGold
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy