-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.java
More file actions
51 lines (37 loc) · 1.28 KB
/
solution.java
File metadata and controls
51 lines (37 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public char[][] rotateTheBox(char[][] boxGrid) {
int m = boxGrid.length;
int n = boxGrid[0].length;
// Process every row
for (int row = 0; row < m; row++) {
// Rightmost empty position
int emptyCol = n - 1;
// Traverse row from right to left
for (int col = n - 1; col >= 0; col--) {
// Obstacle found
if (boxGrid[row][col] == '*') {
// Reset valid falling position
emptyCol = col - 1;
}
// Stone found
else if (boxGrid[row][col] == '#') {
// Remove stone
boxGrid[row][col] = '.';
// Move stone to valid position
boxGrid[row][emptyCol] = '#';
// Update next empty spot
emptyCol--;
}
}
}
// Rotated matrix
char[][] rotated = new char[n][m];
// Rotate clockwise
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
rotated[j][m - 1 - i] = boxGrid[i][j];
}
}
return rotated;
}
}