forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-sum-queries.py
29 lines (26 loc) · 945 Bytes
/
maximum-sum-queries.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
# Time: O(nlogn + mlogm + mlogn)
# Space: O(n + m)
import itertools
import bisect
# sort, mono stack, binary search
class Solution(object):
def maximumSumQueries(self, nums1, nums2, queries):
"""
:type nums1: List[int]
:type nums2: List[int]
:type queries: List[List[int]]
:rtype: List[int]
"""
pairs = sorted(((i, j) for i, j in itertools.izip(nums1, nums2)))
result = [0]*len(queries)
stk = []
for x, y, i in sorted(((x, y, i) for i, (x, y) in enumerate(queries)), reverse=True):
while pairs and pairs[-1][0] >= x:
a, b = pairs.pop()
while stk and stk[-1][1] <= a+b:
stk.pop()
if not stk or stk[-1][0] < b:
stk.append((b, a+b))
j = bisect.bisect_left(stk, (y,))
result[i] = stk[j][1] if j != len(stk) else -1
return result