Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78287f9

Browse files
authored
Added task 699.
1 parent 0197502 commit 78287f9

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0601_0700.s0699_falling_squares;
2+
3+
// #Hard #Array #Ordered_Set #Segment_Tree
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.Set;
9+
import java.util.TreeSet;
10+
11+
public class Solution {
12+
public List<Integer> fallingSquares(int[][] positions) {
13+
// Coordinate compression using TreeSet
14+
Set<Integer> unique = new TreeSet<>();
15+
for (int[] square : positions) {
16+
unique.add(square[0]);
17+
unique.add(square[0] + square[1] - 1);
18+
}
19+
// converted the TreeSet to a List
20+
List<Integer> sorted = new ArrayList<>(unique);
21+
// Storing the max heights for compressed coordinates
22+
int[] heights = new int[sorted.size()];
23+
// Our answer list
24+
List<Integer> list = new ArrayList<>(positions.length);
25+
// Global Max
26+
int max = 0;
27+
for (int[] square : positions) {
28+
// coordinate compression lookup
29+
int x1 = Collections.binarySearch(sorted, square[0]);
30+
int x2 = Collections.binarySearch(sorted, square[0] + square[1] - 1);
31+
// get the current max for the interval between x1 and x2
32+
int current = 0;
33+
for (int i = x1; i <= x2; i++) {
34+
current = Math.max(current, heights[i]);
35+
}
36+
// add the new square on the top
37+
current += square[1];
38+
// update the interval with the new value
39+
for (int i = x1; i <= x2; i++) {
40+
heights[i] = current;
41+
}
42+
// recalculate the global max
43+
max = Math.max(max, current);
44+
list.add(max);
45+
}
46+
return list;
47+
}
48+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
699\. Falling Squares
2+
3+
Hard
4+
5+
There are several squares being dropped onto the X-axis of a 2D plane.
6+
7+
You are given a 2D integer array `positions` 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>.
8+
9+
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 **on the top side of another square** or **on the X-axis**. 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.
10+
11+
After each square is dropped, you must record the **height of the current tallest stack of squares**.
12+
13+
Return _an integer array_ `ans` _where_ `ans[i]` _represents the height described above after dropping the_ <code>i<sup>th</sup></code> _square_.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/04/28/fallingsq1-plane.jpg)
18+
19+
**Input:** positions = [[1,2],[2,3],[6,1]]
20+
21+
**Output:** [2,5,5]
22+
23+
**Explanation:**
24+
25+
After the first drop, the tallest stack is square 1 with a height of 2.
26+
27+
After the second drop, the tallest stack is squares 1 and 2 with a height of 5.
28+
29+
After the third drop, the tallest stack is still squares 1 and 2 with a height of 5.
30+
31+
Thus, we return an answer of [2, 5, 5].
32+
33+
**Example 2:**
34+
35+
**Input:** positions = [[100,100],[200,100]]
36+
37+
**Output:** [100,100]
38+
39+
**Explanation:**
40+
41+
After the first drop, the tallest stack is square 1 with a height of 100.
42+
43+
After the second drop, the tallest stack is either square 1 or square 2, both with heights of 100.
44+
45+
Thus, we return an answer of [100, 100].
46+
47+
Note that square 2 only brushes the right side of square 1, which does not count as landing on it.
48+
49+
**Constraints:**
50+
51+
* `1 <= positions.length <= 1000`
52+
* <code>1 <= left<sub>i</sub> <= 10<sup>8</sup></code>
53+
* <code>1 <= sideLength<sub>i</sub> <= 10<sup>6</sup></code>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0601_0700.s0699_falling_squares;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.Arrays;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void fallingSquares() {
12+
assertThat(
13+
new Solution().fallingSquares(new int[][] {{1, 2}, {2, 3}, {6, 1}}),
14+
equalTo(Arrays.asList(2, 5, 5)));
15+
}
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /