699. 掉落的方块
题目链接:699. 掉落的方块
代码如下:
//参考链接:https://leetcode.cn/problems/falling-squares/solutions/1514366/diao-luo-de-fang-kuai-by-leetcode-soluti-2dmw
class Solution
{
public:
vector<int> fallingSquares(vector<vector<int>>& positions)
{
vector<int> heights(positions.size());
for(int i=0;i<positions.size();i++)
{
int left1=positions[i][0],right1=positions[i][0]+positions[i][1]-1;
heights[i]=positions[i][1];
for(int j=0;j<i;j++)
{
int left2=positions[j][0],right2=positions[j][0]+positions[j][1]-1;
if(right1>=left2&&right2>=left1)
{
heights[i]=max(heights[i],heights[j]+positions[i][1]);
}
}
}
for(int i=1;i<positions.size();i++)
{
heights[i]=max(heights[i],heights[i-1]);
}
return heights;
}
};