kt: add «1861. Rotating the Box»
URL: https://leetcode.com/problems/rotating-the-box/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
8e196a299a
commit
da9d7ea742
1 changed files with 24 additions and 0 deletions
24
kt/rotating-the-box.kt
Normal file
24
kt/rotating-the-box.kt
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
class Solution {
|
||||||
|
fun rotateTheBox(box: Array<CharArray>): Array<CharArray> {
|
||||||
|
val (rows, cols) = box.size to box[0].size
|
||||||
|
val rotated = Array(cols) { CharArray(rows) { '.' } }
|
||||||
|
|
||||||
|
(0..<rows).forEach { y ->
|
||||||
|
(0..<cols).reversed().fold(cols - 1) { lowestEmpty, x ->
|
||||||
|
when (box[y][x]) {
|
||||||
|
'#' -> {
|
||||||
|
rotated[lowestEmpty][rows - y - 1] = '#'
|
||||||
|
lowestEmpty - 1
|
||||||
|
}
|
||||||
|
'*' -> {
|
||||||
|
rotated[x][rows - y - 1] = '*'
|
||||||
|
x - 1
|
||||||
|
}
|
||||||
|
else -> lowestEmpty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rotated
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue