-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathminimum-operations-to-make-subarray-elements-equal.py
50 lines (46 loc) · 1.61 KB
/
minimum-operations-to-make-subarray-elements-equal.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Time: O(nlogk)
# Space: O(k)
from sortedcontainers import SortedList
# math, two pointers, sliding window, sorted list
class Solution(object):
def minOperations(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
def rebalance(total_left, total_right):
if len(left)+1 < len(right):
x, i = right.pop(0)
total_right -= x
left.add((-x, -i))
total_left += x
elif len(left) > len(right):
x, i = left.pop(0)
total_left -= -x
right.add((-x, -i))
total_right += -x
return total_left, total_right
result = float("inf")
left = SortedList()
right = SortedList()
total_left = total_right = 0
for i, x in enumerate(nums):
if left and -left[0][0] > x:
left.add((-x, -i))
total_left += x
else:
right.add((x, i))
total_right += x
total_left, total_right = rebalance(total_left, total_right)
if i-(k-1) >= 0:
result = min(result, (total_right-(right[0][0] if k%2 else 0))-total_left)
j, y = i-(k-1), nums[i-(k-1)]
if (-y, -j) in left:
left.remove((-y, -j))
total_left -= y
else:
right.remove((y, j))
total_right -= y
total_left, total_right = rebalance(total_left, total_right)
return result