Skip to content

Commit aba904e

Browse files
authored
Update 699-falling-squares.js
1 parent 660e9e1 commit aba904e

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

699-falling-squares.js

+55
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,58 @@ function getHeight(intervals, cur) {
3131
intervals.push(cur)
3232
return cur.height
3333
}
34+
35+
// another
36+
37+
/**
38+
* @param {number[][]} positions
39+
* @return {number[]}
40+
*/
41+
var fallingSquares = function (positions) {
42+
let ranges = [{ left: 0, height: 0, right: 1e8 + 1e6 }], rtn = [], max = 0
43+
44+
outer:
45+
for (let [left, length] of positions) {
46+
let curHeight = 0, startI = -1, right = left + length, newRanges = []
47+
for (let i = 0; i < ranges.length; i++) {
48+
let range = ranges[i]
49+
if (left < range.right && startI == -1) {
50+
startI = i
51+
// left part
52+
if (left != range.left) {
53+
newRanges.push({
54+
left: range.left,
55+
height: range.height,
56+
right: left
57+
})
58+
}
59+
}
60+
if (startI != -1) {
61+
curHeight = Math.max(curHeight, range.height)
62+
}
63+
if (right <= range.right) {
64+
// added part
65+
let newHeight = length + curHeight
66+
newRanges.push({
67+
left,
68+
height: newHeight,
69+
right,
70+
})
71+
// right part
72+
if (right != range.right) {
73+
newRanges.push({
74+
left: right,
75+
height: range.height,
76+
right: range.right,
77+
})
78+
}
79+
max = Math.max(newHeight, max)
80+
rtn.push(max)
81+
// replace
82+
ranges.splice(startI, i - startI + 1, ...newRanges)
83+
continue outer
84+
}
85+
}
86+
}
87+
return rtn
88+
};

0 commit comments

Comments
 (0)