LeetCode in Kotlin

3446. Sort Matrix by Diagonals

Medium

You are given an n x n square matrix of integers grid. Return the matrix such that:

Example 1:

Input: grid = [[1,7,3],[9,8,2],[4,5,6]]

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

Explanation:

The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing order:

The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order:

Example 2:

Input: grid = [[0,1],[1,2]]

Output: [[2,1],[1,0]]

Explanation:

The diagonals with a black arrow must be non-increasing, so [0, 2] is changed to [2, 0]. The other diagonals are already in the correct order.

Example 3:

Input: grid = [[1]]

Output: [[1]]

Explanation:

Diagonals with exactly one element are already in order, so no changes are needed.

Constraints:

Solution

class Solution {
    fun sortMatrix(grid: Array<IntArray>): Array<IntArray> {
        val top = 0
        var left = 0
        var right = grid[0].size - 1
        while (top < right) {
            var x = grid[0].size - 1 - left
            val arr = IntArray(left + 1)
            for (i in top..left) {
                arr[i] = grid[i][x++]
            }
            arr.sort()
            x = grid[0].size - 1 - left
            for (i in top..left) {
                grid[i][x++] = arr[i]
            }
            left++
            right--
        }
        var bottom = grid.size - 1
        var x = 0
        while (top <= bottom) {
            val arr = IntArray(bottom + 1)
            for (i in arr.indices) {
                arr[i] = grid[x + i][i]
            }
            arr.sort()
            for (i in arr.indices) {
                grid[x + i][i] = arr[arr.size - 1 - i]
            }
            bottom--
            x++
        }
        return grid
    }
}