forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries-on-a-permutation-with-key.py
More file actions
41 lines (36 loc) · 964 Bytes
/
queries-on-a-permutation-with-key.py
File metadata and controls
41 lines (36 loc) · 964 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
30
31
32
33
34
35
36
37
38
39
40
41
# Time: O(nlogn)
# Space: O(n)
class BIT(object): # Fenwick Tree, 1-indexed
def __init__(self, n):
self.__bit = [0] * n
def add(self, i, val):
while i < len(self.__bit):
self.__bit[i] += val
i += (i & -i)
def sum(self, i):
result = 0
while i > 0:
result += self.__bit[i]
i -= (i & -i)
return result
class Solution(object):
def processQueries(self, queries, m):
"""
:type queries: List[int]
:type m: int
:rtype: List[int]
"""
bit = BIT(2*m+1)
lookup = {}
for i in xrange(1, m+1):
bit.add(m+i, 1)
lookup[i] = m+i
result, curr = [], m
for q in queries:
i = lookup.pop(q)
result.append(bit.sum(i-1))
bit.add(i, -1)
lookup[q] = curr
bit.add(curr, 1)
curr -= 1
return result