LeetCode in Kotlin

3033. Modify the Matrix

Easy

Given a 0-indexed m x n integer matrix matrix, create a new 0-indexed matrix called answer. Make answer equal to matrix, then replace each element with the value -1 with the maximum element in its respective column.

Return the matrix answer.

Example 1:

Input: matrix = [[1,2,-1],[4,-1,6],[7,8,9]]

Output: [[1,2,9],[4,8,6],[7,8,9]]

Explanation: The diagram above shows the elements that are changed (in blue).

Example 2:

Input: matrix = [[3,-1],[5,2]]

Output: [[3,2],[5,2]]

Explanation: The diagram above shows the elements that are changed (in blue).

Constraints:

Solution

class Solution {
    fun modifiedMatrix(matrix: Array<IntArray>): Array<IntArray> {
        for (i in matrix.indices) {
            for (j in matrix[0].indices) {
                if (matrix[i][j] == -1) {
                    var y = 0
                    for (ints in matrix) {
                        if (ints[j] > y) {
                            y = ints[j]
                        }
                    }
                    matrix[i][j] = y
                }
            }
        }
        return matrix
    }
}