Falling Squares LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> fallingSquares(vector<vector<int>> &positions) {
4
vector<int> ans;
5
map<int, pair<int, int>> h;
6
int max_height = 0;
7
for (int i = 0; i < positions.size(); ++i) {
8
int left = positions[i][0];
9
int right = left + positions[i][1];
10
int height = 0;
11
for (auto &it : h) {
12
if (it.first >= right) break;
13
if (it.second.first > left) {
14
height = max(height, it.second.second);
15
}
16
}
17
height += positions[i][1];
18
h[left] = {right, height};
19
max_height = max(max_height, height);
20
ans.push_back(max_height);
21
}
22
return ans;
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0