-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathminimum-operations-to-make-elements-within-k-subarrays-equal.py
189 lines (162 loc) · 5.98 KB
/
minimum-operations-to-make-elements-within-k-subarrays-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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Time: O(nlogx + k * n)
# Space: O(n)
from sortedcontainers import SortedList
# two sorted lists, dp
class Solution(object):
def minOperations(self, nums, x, k):
"""
:type nums: List[int]
:type x: int
:type k: int
:rtype: int
"""
class SlidingWindow(object):
def __init__(self):
self.left = SortedList()
self.right = SortedList()
self.total1 = self.total2 = 0
def add(self, val):
if not self.left or val <= self.left[-1]:
self.left.add(val)
self.total1 += val
else:
self.right.add(val)
self.total2 += val
self.rebalance()
def remove(self, val):
if val <= self.left[-1]:
self.left.remove(val)
self.total1 -= val
else:
self.right.remove(val)
self.total2 -= val
self.rebalance()
def rebalance(self):
if len(self.left) < len(self.right):
self.total2 -= self.right[0]
self.total1 += self.right[0]
self.left.add(self.right[0])
self.right.pop(0)
elif len(self.left) > len(self.right)+1:
self.total1 -= self.left[-1]
self.total2 += self.left[-1]
self.right.add(self.left[-1])
self.left.pop()
def median(self):
return self.left[-1]
INF = float("inf")
sw = SlidingWindow()
cost = [INF]*(len(nums)+1)
for i in xrange(len(nums)):
if i-x >= 0:
sw.remove(nums[i-x])
sw.add(nums[i])
if i >= x-1:
cost[i+1] = (sw.median()*len(sw.left)-sw.total1) + (sw.total2-sw.median()*len(sw.right))
dp = [0]*(len(nums)+1)
for i in xrange(k):
new_dp = [INF]*(len(nums)+1)
for j in xrange((i+1)*x, len(nums)+1):
new_dp[j] = min(new_dp[j-1], dp[j-x]+cost[j])
dp = new_dp
return dp[-1]
# Time: O(nlogx + k * n)
# Space: O(n)
import heapq
import collections
# two heaps, dp
class Solution2(object):
def minOperations(self, nums, x, k):
"""
:type nums: List[int]
:type x: int
:type k: int
:rtype: int
"""
class LazyHeap(object):
def __init__(self, sign):
self.heap = []
self.to_remove = collections.defaultdict(int)
self.cnt = 0
self.sign = sign
def push(self, val):
heapq.heappush(self.heap, self.sign*val)
def full_remove(self):
result = []
for x in self.heap:
if x not in self.to_remove:
result.append(x)
continue
self.to_remove[x] -= 1
if not self.to_remove[x]:
del self.to_remove[x]
self.heap[:] = result
heapq.heapify(self.heap)
def remove(self, val):
self.to_remove[self.sign*val] += 1
self.cnt += 1
if self.cnt > len(self.heap)-self.cnt:
self.full_remove()
self.cnt = 0
def pop(self):
self.remove(self.top())
def top(self):
while self.heap and self.heap[0] in self.to_remove:
self.to_remove[self.heap[0]] -= 1
self.cnt -= 1
if self.to_remove[self.heap[0]] == 0:
del self.to_remove[self.heap[0]]
heapq.heappop(self.heap)
return self.sign*self.heap[0]
def __len__(self):
return len(self.heap)-self.cnt
class SlidingWindow(object):
def __init__(self):
self.left = LazyHeap(-1) # max heap
self.right = LazyHeap(+1) # min heap
self.total1 = self.total2 = 0
def add(self, val):
if not self.left or val <= self.left.top():
self.left.push(val)
self.total1 += val
else:
self.right.push(val)
self.total2 += val
self.rebalance()
def remove(self, val):
if val <= self.left.top():
self.left.remove(val)
self.total1 -= val
else:
self.right.remove(val)
self.total2 -= val
self.rebalance()
def rebalance(self):
if len(self.left) < len(self.right):
self.total2 -= self.right.top()
self.total1 += self.right.top()
self.left.push(self.right.top())
self.right.pop()
elif len(self.left) > len(self.right)+1:
self.total1 -= self.left.top()
self.total2 += self.left.top()
self.right.push(self.left.top())
self.left.pop()
def median(self):
return self.left.top()
INF = float("inf")
sw = SlidingWindow()
cost = [INF]*(len(nums)+1)
for i in xrange(len(nums)):
if i-x >= 0:
sw.remove(nums[i-x])
sw.add(nums[i])
if i >= x-1:
cost[i+1] = (sw.median()*len(sw.left)-sw.total1) + (sw.total2-sw.median()*len(sw.right))
dp = [0]*(len(nums)+1)
for i in xrange(k):
new_dp = [INF]*(len(nums)+1)
for j in xrange((i+1)*x, len(nums)+1):
new_dp[j] = min(new_dp[j-1], dp[j-x]+cost[j])
dp = new_dp
return dp[-1]