|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 |
| -/** |
7 |
| - * 699. Falling Squares |
8 |
| - * |
9 |
| - * On an infinite number line (x-axis), we drop given squares in the order they are given. |
10 |
| - * The i-th square dropped (positions[i] = (left, side_length)) is a |
11 |
| - * square with the left-most point being positions[i][0] and sidelength positions[i][1]. |
12 |
| - * The square is dropped with the bottom edge parallel to the number line, and |
13 |
| - * from a higher height than all currently landed squares. We wait for each square to stick before dropping the next. |
14 |
| - * The squares are infinitely sticky on their bottom edge, and will |
15 |
| - * remain fixed to any positive length surface they touch (either the number line or another square). |
16 |
| - * Squares dropped adjacent to each other will not stick together prematurely. |
17 |
| - * Return a list ans of heights. |
18 |
| - * Each height ans[i] represents the current highest height of any square we have dropped, |
19 |
| - * after dropping squares represented by positions[0], positions[1], ..., positions[i]. |
20 |
| -
|
21 |
| - Example 1: |
22 |
| -
|
23 |
| - Input: [[1, 2], [2, 3], [6, 1]] |
24 |
| - Output: [2, 5, 5] |
25 |
| - Explanation: |
26 |
| -
|
27 |
| -
|
28 |
| - After the first drop of positions[0] = [1, 2]: |
29 |
| - _aa |
30 |
| - _aa |
31 |
| - ------- |
32 |
| - The maximum height of any square is 2. |
33 |
| -
|
34 |
| -
|
35 |
| - After the second drop of positions[1] = [2, 3]: |
36 |
| - __aaa |
37 |
| - __aaa |
38 |
| - __aaa |
39 |
| - _aa__ |
40 |
| - _aa__ |
41 |
| - -------------- |
42 |
| - The maximum height of any square is 5. |
43 |
| - The larger square stays on top of the smaller square despite where its center |
44 |
| - of gravity is, because squares are infinitely sticky on their bottom edge. |
45 |
| -
|
46 |
| -
|
47 |
| - After the third drop of positions[1] = [6, 1]: |
48 |
| - __aaa |
49 |
| - __aaa |
50 |
| - __aaa |
51 |
| - _aa |
52 |
| - _aa___a |
53 |
| - -------------- |
54 |
| - The maximum height of any square is still 5. |
55 |
| -
|
56 |
| - Thus, we return an answer of [2, 5, 5]. |
57 |
| -
|
58 |
| -
|
59 |
| - Example 2: |
60 |
| -
|
61 |
| - Input: [[100, 100], [200, 100]] |
62 |
| - Output: [100, 100] |
63 |
| - Explanation: Adjacent squares don't get stuck prematurely - only their bottom edge can stick to surfaces. |
64 |
| -
|
65 |
| - Note: |
66 |
| - 1 <= positions.length <= 1000. |
67 |
| - 1 <= positions[0] <= 10^8. |
68 |
| - 1 <= positions[1] <= 10^6. |
69 |
| - */ |
70 |
| - |
71 | 6 | public class _699 {
|
72 | 7 | public static class Solution1 {
|
73 |
| - /**credit: https://discuss.leetcode.com/topic/107107/easy-understood-o-n-2-solution-with-explanation*/ |
| 8 | + /** |
| 9 | + * credit: https://discuss.leetcode.com/topic/107107/easy-understood-o-n-2-solution-with-explanation |
| 10 | + */ |
74 | 11 | public List<Integer> fallingSquares(int[][] positions) {
|
75 | 12 | List<Interval> intervals = new ArrayList<>();
|
76 | 13 | List<Integer> result = new ArrayList<>();
|
|
0 commit comments