Rotating the Box - Complete Solution Guide
Rotating the Box is LeetCode problem 1861, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an m x n matrix of characters boxGrid representing a side-view of a box. Each cell of the box is one of the following: 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 pos
Detailed Explanation
The problem asks us to simulate rotating a 2D grid (representing a box) by 90 degrees clockwise and then applying gravity, causing stones ('#') to fall to the bottom of the box until they encounter an obstacle ('*'), another stone, or the bottom edge. We need to return the resulting grid after this rotation and gravity simulation. The input is a 2D array of characters, and the output is a new 2D array of characters representing the rotated and adjusted box.
Solution Approach
The solution involves two main steps: first, rotating the input grid 90 degrees clockwise, and second, applying gravity to the rotated grid. The rotation is done by creating a new grid with dimensions transposed and reversing each row. Gravity is applied by iterating through the columns of the rotated grid. For each column, a `write_row` variable tracks the lowest empty position where a stone can fall. As we iterate upwards, if we encounter a stone, we move it to the `write_row` position and update the `write_row`. If we encounter an obstacle, it blocks the fall, and we update `write_row` to be right above the obstacle.
Step-by-Step Algorithm
- Step 1: Determine the dimensions of the input `boxGrid` (m rows, n columns).
- Step 2: Create a new `rotated_box` grid with dimensions n x m. This is where the rotated result will be stored.
- Step 3: Populate `rotated_box` by transposing `boxGrid` and reversing each row. Specifically, `rotated_box[j][m-1-i] = boxGrid[i][j]` does the rotation efficiently.
- Step 4: Iterate through each column `c` of `rotated_box` (from 0 to m-1).
- Step 5: For each column `c`, initialize `write_row` to the bottom row (`n-1`). `write_row` tracks where the next stone should be placed due to gravity.
- Step 6: Iterate upwards through each row `r` in column `c` (from n-1 to 0).
- Step 7: If `rotated_box[r][c]` is an obstacle ('*'), update `write_row` to `r-1`. The obstacle blocks the fall of any stones below.
- Step 8: If `rotated_box[r][c]` is a stone ('#'), move it to the `write_row` position. Set `rotated_box[r][c]` to '.' and `rotated_box[write_row][c]` to '#', and then decrement `write_row`.
- Step 9: Return the `rotated_box`.
Key Insights
- Insight 1: The rotation can be achieved by transposing the matrix and then reversing each row.
- Insight 2: Applying gravity can be simulated by iterating through each column and tracking the lowest available position for a stone to fall.
- Insight 3: Obstacles ('*') remain in place and act as stopping points for the falling stones.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(m*n)
Topics
This problem involves: Array, Two Pointers, Matrix.
Companies
Asked at: Block, Capital One, Commvault, Roblox, Visa.