Skip to content

Commit 0e01ea3

Browse files
committed
New Problem Solution - "1861. Rotating the Box"
1 parent 406e77e commit 0e01ea3

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1861|[Rotating the Box](https://leetcode.com/problems/rotating-the-box/) | [C++](./algorithms/cpp/rotatingTheBox/RotatingTheBox.cpp)|Medium|
1213
|1860|[Incremental Memory Leak](https://leetcode.com/problems/incremental-memory-leak/) | [C++](./algorithms/cpp/incrementalMemoryLeak/IncrementalMemoryLeak.cpp)|Medium|
1314
|1859|[Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence/) | [C++](./algorithms/cpp/sortingTheSentence/SortingTheSentence.cpp)|Easy|
1415
|1857|[Largest Color Value in a Directed Graph](https://leetcode.com/problems/largest-color-value-in-a-directed-graph/) | [C++](./algorithms/cpp/largestColorValueInADirectedGraph/LargestColorValueInADirectedGraph.cpp)|Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Source : https://leetcode.com/problems/rotating-the-box/
2+
// Author : Hao Chen
3+
// Date : 2021-05-22
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given an m x n matrix of characters box representing a side-view of a box. Each cell of the
8+
* box is one of the following:
9+
*
10+
* A stone '#'
11+
* A stationary obstacle '*'
12+
* Empty '.'
13+
*
14+
* The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each
15+
* stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity
16+
* does not affect the obstacles' positions, and the inertia from the box's rotation does not affect
17+
* the stones' horizontal positions.
18+
*
19+
* It is guaranteed that each stone in box rests on an obstacle, another stone, or the bottom of the
20+
* box.
21+
*
22+
* Return an n x m matrix representing the box after the rotation described above.
23+
*
24+
* Example 1:
25+
*
26+
* Input: box = [["#",".","#"]]
27+
* Output: [["."],
28+
* ["#"],
29+
* ["#"]]
30+
*
31+
* Example 2:
32+
*
33+
* Input: box = [["#",".","*","."],
34+
* ["#","#","*","."]]
35+
* Output: [["#","."],
36+
* ["#","#"],
37+
* ["*","*"],
38+
* [".","."]]
39+
*
40+
* Example 3:
41+
*
42+
* Input: box = [["#","#","*",".","*","."],
43+
* ["#","#","#","*",".","."],
44+
* ["#","#","#",".","#","."]]
45+
* Output: [[".","#","#"],
46+
* [".","#","#"],
47+
* ["#","#","*"],
48+
* ["#","*","."],
49+
* ["#",".","*"],
50+
* ["#",".","."]]
51+
*
52+
* Constraints:
53+
*
54+
* m == box.length
55+
* n == box[i].length
56+
* 1 <= m, n <= 500
57+
* box[i][j] is either '#', '*', or '.'.
58+
******************************************************************************************************/
59+
60+
class Solution {
61+
public:
62+
void rotate(vector<vector<char>>& src, vector<vector<char>>& dest) {
63+
int m = src.size();
64+
for(int row=0; row<dest.size(); row++) {
65+
for(int col=0; col<dest[row].size(); col++) {
66+
dest[row][col] = src[m-col-1][row];
67+
}
68+
}
69+
}
70+
void gravity(vector<vector<char>>& box) {
71+
int m = box.size();
72+
int n = box[0].size();
73+
for(int col=0; col<n; col++) {
74+
int bottom = m - 1;
75+
for(int row=m-1; row>=0; row-- ) {
76+
77+
if (box[row][col] == '#') {
78+
box[row][col] = '.';
79+
box[bottom][col] = '#';
80+
bottom--;
81+
}else if (box[row][col] == '*') {
82+
bottom = row-1;
83+
}
84+
}
85+
}
86+
}
87+
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
88+
int row = box.size();
89+
int col = box[0].size();
90+
vector<vector<char>> result(col, vector<char>(row,'.'));
91+
rotate(box, result);
92+
gravity(result);
93+
return result;
94+
}
95+
};

0 commit comments

Comments
 (0)