-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallingSquares.cpp
More file actions
33 lines (32 loc) · 1.17 KB
/
fallingSquares.cpp
File metadata and controls
33 lines (32 loc) · 1.17 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
// Source: https://leetcode.com/problems/falling-squares/
// Author: Miao Zhang
// Date: 2021-03-02
class Solution {
public:
vector<int> fallingSquares(vector<vector<int>>& positions) {
vector<int> res;
map<pair<int, int>, int> m;
int curMax = 0;
for (auto &pos: positions) {
vector<vector<int>> t;
int side_len = pos[1], start = pos[0], end = start + side_len, h = 0;
auto it = m.upper_bound({start, end});
if (it != m.begin() && (--it)->first.second <= start) ++it;
while (it != m.end() && it->first.first < end) {
if (start > it->first.first) {
t.push_back({it->first.first, start, it->second});
}
if (end < it->first.second) {
t.push_back({end, it->first.second, it->second});
}
h = max(h, it->second);
it = m.erase(it);
}
m[{start, end}] = h + side_len;
for (auto &e: t) m[{e[0], e[1]}] = e[2];
curMax = max(curMax, h + side_len);
res.push_back(curMax);
}
return res;
}
};