File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ My solutions to LeetCode problems in Kotlin.
2424
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
2525
| [1574](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/) | [Shortest Subarray to be Removed to Make Array Sorted](src/main/kotlin/com/schmoczer/leetcode/_1574/ShortestSubarray.kt) | Medium |
2626
| [1829](https://leetcode.com/problems/maximum-xor-for-each-query/) | [Maximum XOR for Each Query](src/main/kotlin/com/schmoczer/leetcode/_1829/MaximumXorForEachQuery.kt) | Medium |
27+
| [1861](https://leetcode.com/problems/rotating-the-box/) | [Rotating the Box](src/main/kotlin/com/schmoczer/leetcode/_1861/RotatingTheBox.kt) | Medium |
2728
| [1957](https://leetcode.com/problems/delete-characters-to-make-fancy-string/) | [Delete Characters to Make Fancy String](src/main/kotlin/com/schmoczer/leetcode/_1957/DeleteCharactersToMakeFancyString.kt) | Easy |
2829
| [2064](https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/) | [Minimized Maximum of Products Distributed to Any Store](src/main/kotlin/com/schmoczer/leetcode/_2064/MinimizedMaximum.kt) | Medium |
2930
| [2070](https://leetcode.com/problems/most-beautiful-item-for-each-query/) | [Most Beautiful Item for Each Query](src/main/kotlin/com/schmoczer/leetcode/_2070/MostBeautifulItemForEachQuery.kt) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Rotating the Box
2+
3+
You are given an `m x n` matrix of characters `box` representing a side-view of a box. Each cell of the box is one of
4+
the following:
5+
6+
- A stone `'#'`
7+
- A stationary obstacle `'*'`
8+
- Empty `'.'`
9+
10+
The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until
11+
it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and
12+
the inertia from the box's rotation does not affect the stones' horizontal positions.
13+
14+
It is guaranteed that each stone in `box` rests on an obstacle, another stone, or the bottom of the box.
15+
16+
Return an `n x m` matrix representing the box after the rotation described above.
17+
18+
Example 1:
19+
20+
> Input: box = [["#",".","#"]]
21+
>
22+
> Output: [["."],
23+
["#"],
24+
["#"]]
25+
26+
Example 2:
27+
28+
> Input: box = [["#",".","*","."],
29+
["#","#","*","."]]
30+
>
31+
> Output: [["#","."],
32+
["#","#"],
33+
["*","*"],
34+
[".","."]]
35+
36+
Example 3:
37+
38+
> Input: box = [["#","#","*",".","*","."],
39+
["#","#","#","*",".","."],
40+
["#","#","#",".","#","."]]
41+
>
42+
> Output: [[".","#","#"],
43+
[".","#","#"],
44+
["#","#","*"],
45+
["#","*","."],
46+
["#",".","*"],
47+
["#",".","."]]
48+
49+
50+
Constraints:
51+
52+
- `m == box.length`
53+
- `n == box[i].length`
54+
- `1 <= m, n <= 500`
55+
- `box[i][j]` is either `'#'`, `'*'`, or `'.'.`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.schmoczer.leetcode._1861
2+
3+
class RotatingTheBox {
4+
fun rotateTheBox(box: Array<CharArray>): Array<CharArray> {
5+
if (box.isEmpty()) {
6+
return emptyArray()
7+
}
8+
for (row in box) {
9+
var lastStarFound: Int? = null
10+
var lastDotFound: Int? = null
11+
for (i in row.size - 1 downTo 0) {
12+
when (row[i]) {
13+
'.' -> {
14+
if (lastDotFound == null) {
15+
lastDotFound = i
16+
}
17+
}
18+
19+
'*' -> {
20+
lastStarFound = i
21+
lastDotFound = null
22+
}
23+
24+
'#' -> {
25+
if (lastDotFound != null) {
26+
row[lastDotFound] = '#'
27+
row[i] = '.'
28+
lastDotFound--
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
return Array(box[0].size) { x ->
36+
CharArray(box.size) { y ->
37+
box[box.size - y - 1][x]
38+
}
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.schmoczer.leetcode._1861
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertContentEquals
6+
7+
class RotatingTheBoxTest {
8+
private lateinit var sut: RotatingTheBox
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = RotatingTheBox()
13+
}
14+
15+
@Test
16+
fun `matrix with 1 row rotated`() {
17+
val input = arrayOf(charArrayOf('#', '.', '#'))
18+
val expected = arrayOf(
19+
charArrayOf('.'),
20+
charArrayOf('#'),
21+
charArrayOf('#'),
22+
)
23+
24+
val result = sut.rotateTheBox(input)
25+
26+
for (i in 0 until expected.size) {
27+
assertContentEquals(expected[i], result[i])
28+
}
29+
}
30+
31+
@Test
32+
fun `matrix with 2 rows rotated`() {
33+
val input = arrayOf(
34+
charArrayOf('#', '.', '*', '.'),
35+
charArrayOf('#', '#', '*', '.'),
36+
)
37+
val expected = arrayOf(
38+
charArrayOf('#', '.'),
39+
charArrayOf('#', '#'),
40+
charArrayOf('*', '*'),
41+
charArrayOf('.', '.'),
42+
)
43+
44+
val result = sut.rotateTheBox(input)
45+
46+
for (i in 0 until expected.size) {
47+
assertContentEquals(expected[i], result[i])
48+
}
49+
}
50+
51+
@Test
52+
fun `matrix with 3 rows rotated`() {
53+
val input = arrayOf(
54+
charArrayOf('#', '#', '*', '.', '*', '.'),
55+
charArrayOf('#', '#', '#', '*', '.', '.'),
56+
charArrayOf('#', '#', '#', '.', '#', '.'),
57+
)
58+
val expected = arrayOf(
59+
charArrayOf('.', '#', '#'),
60+
charArrayOf('.', '#', '#'),
61+
charArrayOf('#', '#', '*'),
62+
charArrayOf('#', '*', '.'),
63+
charArrayOf('#', '.', '*'),
64+
charArrayOf('#', '.', '.'),
65+
)
66+
67+
val result = sut.rotateTheBox(input)
68+
69+
for (i in 0 until expected.size) {
70+
assertContentEquals(expected[i], result[i])
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)