Post

1972 Rotating The Box

1972 Rotating The Box

Rotating the Box image

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 the following:

1
2
3
A stone '#'
A stationary obstacle '*'
Empty '.'

The box is rotated 90 degrees clockwise, 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 does not affect the obstacles’ positions, and the inertia from the box’s rotation **does not **affect the stones’ horizontal positions.

It is guaranteed that each stone in box rests on an obstacle, another stone, or the bottom of the box.

Return an *n x m matrix representing the box after the rotation described above*.

 

Example 1:

image

1
2
3
4
5
6
**Input:** box = [["#",".","#"]]
**Output:** [["."],
         ["#"],
         ["#"]]

Example 2:

image

1
2
3
4
5
6
7
8
**Input:** box = [["#",".","*","."],
              ["#","#","*","."]]
**Output:** [["#","."],
         ["#","#"],
         ["*","*"],
         [".","."]]

Example 3:

image

1
2
3
4
5
6
7
8
9
10
11
**Input:** box = [["#","#","*",".","*","."],
              ["#","#","#","*",".","."],
              ["#","#","#",".","#","."]]
**Output:** [[".","#","#"],
         [".","#","#"],
         ["#","#","*"],
         ["#","*","."],
         ["#",".","*"],
         ["#",".","."]]

 

Constraints:

1
2
3
4
m == box.length
n == box[i].length
1 <= m, n <= 500
box[i][j] is either '#', '*', or '.'.
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
62
63
64
65
66

class Solution:
    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
        ans = []
        for rows in box:
            start = 0
            end = len(rows) - 1
            temp = end
            prev = "."
            res = []
            while(temp >= 0):
                if rows[temp] == "#":
                    m = 0
                    while(len(res) > 0 and res[-1] == "."):
                        m += 1
                        res.pop(-1)
                    res.append("#")
                    while(m > 0):
                        res.append(".")
                        m -= 1

                    temp -= 1
                    continue
                if rows[temp] == ".":
                    res.append(".")
                    temp -= 1
                    continue
                if rows[temp] == "*":
                    res.append("*")
                    temp -= 1
                    continue
            # fill empty at end
            #print(res)
            while(len(res) < len(rows)):
                res.append(".")
            # reverse
            res.reverse()
            ans.append(copy.deepcopy(res))
        ans.reverse()
        row = 0
        col = 0
        result = []
        t = []
        #print(ans)
        while(col < len(ans[0])  and row < len(ans)):
            
            if row == len(ans) - 1:
                t.append(ans[row][col])
                col += 1
                row = 0
                result.append(copy.deepcopy(t))
                t = []
                continue
            t.append(ans[row][col])
            #print(t)
            row += 1
        return result

                    
                

        



This post is licensed under CC BY 4.0 by the author.