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
    }
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy