classSolution: deffallingSquares(self, positions: List[List[int]]) -> List[int]: ground = [[1,100000000,0]] ans = [] for i inrange(0,len(positions)): left = positions[i][0] right = left + positions[i][1] - 1# 获得我们需要的这个方块的左右边界,然后现在就是需要得到这个方块左右边界当前的最大值。 maxheight = 0 for j inrange(0,len(ground)): l = ground[j][0] r = ground[j][1] if (l<= left and r >=left) or (left <= l and r <= right) or (l<=right and r >=right): # 某一个区块的有边界在 if ground[j][2] > maxheight: maxheight = ground[j][2] # 找到了当前的最大值。 maxheight += positions[i][1] # 最大高度肯定是加上去了。那么下一步就是更新当前的这个ground数组。 # print(left,right,maxheight) tmp = [] for j inrange(0,len(ground)): if ground[j][1] < left: tmp.append(ground[j]) if ground[j][0] > right: tmp.append(ground[j]) if ground[j][0] == left: tmp.append([left,right,maxheight]) if ground[j][0] < left and ground[j][1] >= left: tmp.append([ground[j][0],left - 1,ground[j][2]]) tmp.append([left,right,maxheight]) if ground[j][0] <= right and ground[j][1] > right: tmp.append([right+1,ground[j][1],ground[j][2]]) ground = tmp.copy() # print(ground) iflen(ans) != 0: maxheight = max(maxheight,ans[-1]) ans.append(maxheight) # 答案加进去 return ans