-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.cpp
More file actions
61 lines (48 loc) · 1.6 KB
/
solution.cpp
File metadata and controls
61 lines (48 loc) · 1.6 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
52
53
54
55
56
57
58
59
60
61
class Solution
{
public:
vector<vector<char>> rotateTheBox(vector<vector<char>> &boxGrid)
{
int m = boxGrid.size();
int n = boxGrid[0].size();
// Process every row independently
for (int row = 0; row < m; row++)
{
// This points to the rightmost empty position
// where the next stone can fall
int emptyCol = n - 1;
// Traverse from right to left
for (int col = n - 1; col >= 0; col--)
{
// Obstacle blocks movement
if (boxGrid[row][col] == '*')
{
// Stones can only fall before obstacle
emptyCol = col - 1;
}
// Found a stone
else if (boxGrid[row][col] == '#')
{
// Remove stone from current position
boxGrid[row][col] = '.';
// Put stone at the valid empty position
boxGrid[row][emptyCol] = '#';
// Next stone should go one step left
emptyCol--;
}
}
}
// Create rotated matrix
vector<vector<char>> rotated(n, vector<char>(m));
// Rotate clockwise
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
// Standard clockwise rotation formula
rotated[j][m - 1 - i] = boxGrid[i][j];
}
}
return rotated;
}
};