Skip to content

Commit

Permalink
[LeetCode] Add January 23.
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da committed Jan 23, 2021
1 parent 96fd571 commit eb63346
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions solved/LeetCode/Challenges/2020/January/23.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
fun diagonalSort(mat: Array<IntArray>): Array<IntArray> {
val rows = mat.size
val cols = mat[0].size

fun fix(stI: Int, stJ: Int) {
var i = stI;
var j = stJ;
var nums = ArrayDeque<Int>()
while (i < rows && j < cols) {
nums.addLast(mat[i][j])
i++
j++
}
val s = ArrayDeque(nums.sorted())

j = stJ;
i = stI;
while (i < rows && j < cols) {
mat[i][j] = s.removeFirst()
i++
j++
}
}

for (i in 0 until rows) {
fix(i, 0)
}
for (i in 1 until cols) {
fix(0, i)
}
return mat
}
}

0 comments on commit eb63346

Please sign in to comment.