forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
32 lines (28 loc) · 855 Bytes
/
Solution.py
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
30
31
32
class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
def update(self, x, delta):
while x <= self.n:
self.c[x] = max(self.c[x], delta)
x += x & -x
def query(self, x):
s = 0
while x:
s = max(s, self.c[x])
x -= x & -x
return s
class Solution:
def bestSeqAtIndex(self, height: List[int], weight: List[int]) -> int:
arr = list(zip(height, weight))
arr.sort(key=lambda x: (x[0], -x[1]))
alls = sorted({w for _, w in arr})
m = {w: i for i, w in enumerate(alls, 1)}
tree = BinaryIndexedTree(len(m))
ans = 1
for _, w in arr:
x = m[w]
t = tree.query(x - 1) + 1
ans = max(ans, t)
tree.update(x, t)
return ans