Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a12331e

Browse files
committed
Add solution #699
1 parent bc1d403 commit a12331e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

0699-falling-squares.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 699. Falling Squares
3+
* https://leetcode.com/problems/falling-squares/
4+
* Difficulty: Hard
5+
*
6+
* There are several squares being dropped onto the X-axis of a 2D plane.
7+
*
8+
* You are given a 2D integer array positions where positions[i] = [lefti, sideLengthi]
9+
* represents the ith square with a side length of sideLengthi that is dropped with its
10+
* left edge aligned with X-coordinate lefti.
11+
*
12+
* Each square is dropped one at a time from a height above any landed squares. It then
13+
* falls downward (negative Y direction) until it either lands on the top side of another
14+
* square or on the X-axis. A square brushing the left/right side of another square does
15+
* not count as landing on it. Once it lands, it freezes in place and cannot be moved.
16+
*
17+
* After each square is dropped, you must record the height of the current tallest stack
18+
* of squares.
19+
*
20+
* Return an integer array ans where ans[i] represents the height described above after
21+
* dropping the ith square.
22+
*/
23+
24+
/**
25+
* @param {number[][]} positions
26+
* @return {number[]}
27+
*/
28+
var fallingSquares = function(positions) {
29+
const map = new Map();
30+
const result = [];
31+
let max = 0;
32+
33+
for (const [left, side] of positions) {
34+
const right = left + side;
35+
let height = 0;
36+
37+
for (const [start, [i, n]] of map) {
38+
const end = start + i;
39+
if (right > start && left < end) {
40+
height = Math.max(height, n);
41+
}
42+
}
43+
44+
height += side;
45+
map.set(left, [side, height]);
46+
max = Math.max(max, height);
47+
result.push(max);
48+
}
49+
50+
return result;
51+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@
528528
696|[Count Binary Substrings](./0696-count-binary-substrings.js)|Easy|
529529
697|[Degree of an Array](./0697-degree-of-an-array.js)|Easy|
530530
698|[Partition to K Equal Sum Subsets](./0698-partition-to-k-equal-sum-subsets.js)|Medium|
531+
699|[Falling Squares](./0699-falling-squares.js)|Hard|
531532
700|[Search in a Binary Search Tree](./0700-search-in-a-binary-search-tree.js)|Easy|
532533
701|[Insert into a Binary Search Tree](./0701-insert-into-a-binary-search-tree.js)|Medium|
533534
703|[Kth Largest Element in a Stream](./0703-kth-largest-element-in-a-stream.js)|Easy|

0 commit comments

Comments
 (0)