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

g0401_0500.s0498_diagonal_traverse.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0401_0500.s0498_diagonal_traverse

// #Medium #Array #Matrix #Simulation #2023_01_05_Time_430_ms_(61.54%)_Space_72.5_MB_(50.00%)

class Solution {
    fun findDiagonalOrder(mat: Array): IntArray {
        val m = mat.size
        val n = mat[0].size
        val output = IntArray(m * n)
        var idx = 0
        for (diag in 0..m + n - 2) {
            if (diag % 2 == 0) {
                for (k in Math.max(0, diag - m + 1)..Math.min(diag, n - 1)) {
                    output[idx++] = mat[diag - k][k]
                }
            } else {
                for (k in Math.max(0, diag - n + 1)..Math.min(diag, m - 1)) {
                    output[idx++] = mat[k][diag - k]
                }
            }
        }
        return output
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy