1132. Sand Settles
A quarry conveyor tilts so loose sand slides toward the bottom of every column. The board grid is an m x n array of characters: '*' is a grain of sand, '#' is fixed rock and '.' is empty space. Every grain falls straight down until it rests on rock, on another grain, or on the bottom row. Rocks never move and grains never move sideways.
Modify grid in place so it shows the settled state; the method returns nothing. Columns are independent, so scan each one from the bottom with a write pointer that resets to just above every rock. Expected cost is O(m*n) time and O(1) extra space.
Example 1
- Input:
- grid = [["*",".","*"],[".",".","."],[".","#","."]]
- Output:
- [[".",".","."],[".",".","."],["*","#","*"]]
- Explanation:
Column 1 holds only a rock, so it is unchanged. The grains in columns 0 and 2 fall to the bottom row, giving the settled board.
Example 2
- Input:
- grid = [["*","*"],["#","."],["*","."],[".","."]]
- Output:
- [["*","."],["#","."],[".","."],["*","*"]]
- Explanation:
The top grain in column 0 sits on the rock, the grain below the rock falls to the bottom row, and the grain in column 1 falls all the way down.
Constraints
1 ≤ m, n ≤ 50
Every cell of grid is '*', '#' or '.'
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Graded
- Your answer is read from
gridafter your method returns.
Expected complexity
- Time
- O(m*n)
- Space
- O(1)