Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 71ddba0

Browse files
1
1 parent c2d9fea commit 71ddba0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎src/main/kotlin/p34xx/Problem3446.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package p34xx
2+
3+
import util.expect
4+
5+
fun main() {
6+
class Solution {
7+
fun sortMatrix(grid: Array<IntArray>): Array<IntArray> {
8+
return grid.indices.flatMap { r ->
9+
grid[r].indices.map { r to it }
10+
}.groupBy { it.first - it.second }.flatMap { (diff, positions) ->
11+
val d = diff * 2 + 1
12+
13+
val sorted = positions.sortedWith { (r1, c1), (r2, c2) ->
14+
(grid[r2][c2] - grid[r1][c1]) * d
15+
}
16+
17+
positions.indices.map { positions[it] to sorted[it] }
18+
}.toMap().let {
19+
Array(grid.size) { r ->
20+
IntArray(grid[r].size) { c ->
21+
it[r to c]?.let { (tr, tc) ->
22+
grid[tr][tc]
23+
} ?: 0
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
expect {
31+
Solution().sortMatrix(
32+
arrayOf()
33+
)
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /