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

g0201_0300.s0221_maximal_square.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.28
Show newest version
package g0201_0300.s0221_maximal_square

// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix
// #Dynamic_Programming_I_Day_16 #Big_O_Time_O(m*n)_Space_O(m*n)
// #2022_09_10_Time_614_ms_(44.00%)_Space_76.2_MB_(65.33%)

class Solution {
    fun maximalSquare(matrix: Array): Int {
        val m = matrix.size
        if (m == 0) {
            return 0
        }
        val n: Int = matrix[0].size
        if (n == 0) {
            return 0
        }
        val dp = Array(m + 1) { IntArray(n + 1) }
        var max = 0
        for (i in 0 until m) {
            for (j in 0 until n) {
                if (matrix[i][j] == '1') {
                    // 1 + minimum from cell above, cell to the left, cell diagonal upper-left
                    val next = 1 + Math.min(dp[i][j], Math.min(dp[i + 1][j], dp[i][j + 1]))
                    // keep track of the maximum value seen
                    if (next > max) {
                        max = next
                    }
                    dp[i + 1][j + 1] = next
                }
            }
        }
        return max * max
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy