-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFalling_Squares.cpp
More file actions
63 lines (54 loc) · 1.83 KB
/
Falling_Squares.cpp
File metadata and controls
63 lines (54 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#define MAX 1000000000
class SegmentNode{
public:
int l, r;
int height;
SegmentNode *left, *right;
SegmentNode(int l, int r, int height){
this->l = l;
this->r = r;
this->height = height;
left = NULL;
right = NULL;
}
};
class Solution {
public:
void updateRange(SegmentNode *node, int l, int r, int height){
if(l > node->r || r < node->l)
return;
if(l <= node->l && r >= node->r){
node->height = height;
node->left = NULL;
node->right = NULL;
return;
}
int mid = node->l + (node->r - node->l)/2;
if(node->left == NULL){
node->left = new SegmentNode(node->l, mid, node->height);
node->right = new SegmentNode(mid + 1, node->r, node->height);
}
updateRange(node->left, l, r, height);
updateRange(node->right, l, r, height);
node->height = max(node->left->height, node->right->height);
}
int query(SegmentNode *node, int l, int r){
if(l > node->r || r < node->l)
return 0;
if((l <= node->l && r >= node->r) || node->left == NULL)
return node->height;
return max(query(node->left, l, r), query(node->right, l, r));
}
vector<int> fallingSquares(vector<vector<int>>& positions) {
SegmentNode *root = new SegmentNode(0, MAX, 0);
vector < int > ans;
int result = 0;
for(auto position : positions){
int currHeight = query(root, position[0], position[0] + position[1] - 1);
updateRange(root, position[0], position[0] + position[1] - 1, currHeight + position[1]);
result = max(result, currHeight + position[1]);
ans.push_back(result);
}
return ans;
}
};