Skip to content

Commit bcc3637

Browse files
committed
[LeetCode Sync] Runtime - 1920 ms (43.61%), Memory - 29 MB (43.77%)
1 parent 9c7d1b5 commit bcc3637

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

1972-rotating-the-box/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<p>You are given an <code>m x n</code> matrix of characters <code>box</code> representing a side-view of a box. Each cell of the box is one of the following:</p>
2+
3+
<ul>
4+
<li>A stone <code>&#39;#&#39;</code></li>
5+
<li>A stationary obstacle <code>&#39;*&#39;</code></li>
6+
<li>Empty <code>&#39;.&#39;</code></li>
7+
</ul>
8+
9+
<p>The box is rotated <strong>90 degrees clockwise</strong>, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity <strong>does not</strong> affect the obstacles&#39; positions, and the inertia from the box&#39;s rotation <strong>does not </strong>affect the stones&#39; horizontal positions.</p>
10+
11+
<p>It is <strong>guaranteed</strong> that each stone in <code>box</code> rests on an obstacle, another stone, or the bottom of the box.</p>
12+
13+
<p>Return <em>an </em><code>n x m</code><em> matrix representing the box after the rotation described above</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcodewithstones.png" style="width: 300px; height: 150px;" /></p>
19+
20+
<pre>
21+
<strong>Input:</strong> box = [[&quot;#&quot;,&quot;.&quot;,&quot;#&quot;]]
22+
<strong>Output:</strong> [[&quot;.&quot;],
23+
&nbsp; [&quot;#&quot;],
24+
&nbsp; [&quot;#&quot;]]
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcode2withstones.png" style="width: 375px; height: 195px;" /></p>
30+
31+
<pre>
32+
<strong>Input:</strong> box = [[&quot;#&quot;,&quot;.&quot;,&quot;*&quot;,&quot;.&quot;],
33+
&nbsp; [&quot;#&quot;,&quot;#&quot;,&quot;*&quot;,&quot;.&quot;]]
34+
<strong>Output:</strong> [[&quot;#&quot;,&quot;.&quot;],
35+
&nbsp; [&quot;#&quot;,&quot;#&quot;],
36+
&nbsp; [&quot;*&quot;,&quot;*&quot;],
37+
&nbsp; [&quot;.&quot;,&quot;.&quot;]]
38+
</pre>
39+
40+
<p><strong class="example">Example 3:</strong></p>
41+
42+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcode3withstone.png" style="width: 400px; height: 218px;" /></p>
43+
44+
<pre>
45+
<strong>Input:</strong> box = [[&quot;#&quot;,&quot;#&quot;,&quot;*&quot;,&quot;.&quot;,&quot;*&quot;,&quot;.&quot;],
46+
&nbsp; [&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;*&quot;,&quot;.&quot;,&quot;.&quot;],
47+
&nbsp; [&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;.&quot;,&quot;#&quot;,&quot;.&quot;]]
48+
<strong>Output:</strong> [[&quot;.&quot;,&quot;#&quot;,&quot;#&quot;],
49+
&nbsp; [&quot;.&quot;,&quot;#&quot;,&quot;#&quot;],
50+
&nbsp; [&quot;#&quot;,&quot;#&quot;,&quot;*&quot;],
51+
&nbsp; [&quot;#&quot;,&quot;*&quot;,&quot;.&quot;],
52+
&nbsp; [&quot;#&quot;,&quot;.&quot;,&quot;*&quot;],
53+
&nbsp; [&quot;#&quot;,&quot;.&quot;,&quot;.&quot;]]
54+
</pre>
55+
56+
<p>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li><code>m == box.length</code></li>
61+
<li><code>n == box[i].length</code></li>
62+
<li><code>1 &lt;= m, n &lt;= 500</code></li>
63+
<li><code>box[i][j]</code> is either <code>&#39;#&#39;</code>, <code>&#39;*&#39;</code>, or <code>&#39;.&#39;</code>.</li>
64+
</ul>

1972-rotating-the-box/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
3+
ROWS, COLS = len(box), len(box[0])
4+
5+
for r in range(ROWS):
6+
i = COLS- 1 # transpose counter
7+
for c in reversed(range(COLS)):
8+
if box[r][c] == "#":
9+
box[r][c], box[r][i] = box[r][i], box[r][c]
10+
i -= 1 # shift to left
11+
elif box[r][c] == "*":
12+
i = c-1
13+
14+
15+
# rotate the box
16+
res = []
17+
18+
for c in range(COLS):
19+
col = [] # this is row after rotation
20+
for r in reversed(range(ROWS)):
21+
col.append(box[r][c])
22+
23+
res.append(col)
24+
25+
return res
26+

0 commit comments

Comments
 (0)