-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallingSquares.py
More file actions
29 lines (22 loc) · 842 Bytes
/
fallingSquares.py
File metadata and controls
29 lines (22 loc) · 842 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/falling-squares/
# Author: Miao Zhang
# Date: 2021-03-02
class Solution:
def fallingSquares(self, positions: List[List[int]]) -> List[int]:
coords = set()
for left, size in positions:
coords.add(left)
coords.add(left + size - 1)
index = {x: i for i, x in enumerate(sorted(coords))}
res = []
heights = [0] * len(index)
for l, s in positions:
start = index[l]
end = index[l + s - 1]
maximum_height = max(heights[start:end + 1])
for i in range(start, end+1):
heights[i] = maximum_height + s
res.append(max(heights))
return res