Class Solution

java.lang.Object
g0601_0700.s0699_falling_squares.Solution

public class Solution extends Object
699 - Falling Squares.<p>Hard</p> <p>There are several squares being dropped onto the X-axis of a 2D plane.</p> <p>You are given a 2D integer array <code>positions</code> where <code>positions[i] = [left<sub>i</sub>, sideLength<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> square with a side length of <code>sideLength<sub>i</sub></code> that is dropped with its left edge aligned with X-coordinate <code>left<sub>i</sub></code>.</p> <p>Each square is dropped one at a time from a height above any landed squares. It then falls downward (negative Y direction) until it either lands <strong>on the top side of another square</strong> or <strong>on the X-axis</strong>. A square brushing the left/right side of another square does not count as landing on it. Once it lands, it freezes in place and cannot be moved.</p> <p>After each square is dropped, you must record the <strong>height of the current tallest stack of squares</strong>.</p> <p>Return <em>an integer array</em> <code>ans</code> <em>where</em> <code>ans[i]</code> <em>represents the height described above after dropping the</em> <code>i<sup>th</sup></code> <em>square</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/04/28/fallingsq1-plane.jpg" alt="" /></p> <p><strong>Input:</strong> positions = [[1,2],[2,3],[6,1]]</p> <p><strong>Output:</strong> [2,5,5]</p> <p><strong>Explanation:</strong></p> <p>After the first drop, the tallest stack is square 1 with a height of 2.</p> <p>After the second drop, the tallest stack is squares 1 and 2 with a height of 5.</p> <p>After the third drop, the tallest stack is still squares 1 and 2 with a height of 5.</p> <p>Thus, we return an answer of [2, 5, 5].</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> positions = [[100,100],[200,100]]</p> <p><strong>Output:</strong> [100,100]</p> <p><strong>Explanation:</strong></p> <p>After the first drop, the tallest stack is square 1 with a height of 100.</p> <p>After the second drop, the tallest stack is either square 1 or square 2, both with heights of 100.</p> <p>Thus, we return an answer of [100, 100].</p> <p>Note that square 2 only brushes the right side of square 1, which does not count as landing on it.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= positions.length <= 1000</code></li> <li><code>1 <= left<sub>i</sub> <= 10<sup>8</sup></code></li> <li><code>1 <= sideLength<sub>i</sub> <= 10<sup>6</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • fallingSquares

      public List<Integer> fallingSquares(int[][] positions)