Most Beautiful Item for Each Query LeetCode solution in Python

1
# Runtime: 2544 ms (Top 32.35%) | Memory: 73.6 MB (Top 48.94%)
2
class Solution:
3
def maximumBeauty(self, items: List[List[int]], queries: List[int]) -> List[int]:
4

5
items.sort()
6
dic = dict()
7
res = []
8
gmax = 0
9
for p, b in items:
10
gmax = max(b, gmax)
11
dic[p] = gmax
12

13
keys = sorted(dic.keys())
14
for q in queries:
15
ind = bisect.bisect_left(keys, q)
16
if ind < len(keys) and keys[ind] == q:
17
res.append(dic[q])
18
elif ind == 0:
19
res.append(0)
20
else:
21
res.append(dic[keys[ind - 1]])
22

23
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0