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