forked from Revnth/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
46 lines (34 loc) · 882 Bytes
/
test.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
# Python3 code to count the change required to
# convert the array into non-increasing array
from queue import PriorityQueue
def DecreasingArray(a, n):
ss, dif = (0,0)
# min heap
pq = PriorityQueue()
# Here in the loop we will
# check that whether the upcoming
# element of array is less than top
# of priority queue. If yes then we
# calculate the difference. After
# that we will remove that element
# and push the current element in
# queue. And the sum is incremented
# by the value of difference
for i in range(n):
tmp = 0
if not pq.empty():
tmp = pq.get()
pq.put(tmp)
if not pq.empty() and tmp < a[i]:
dif = a[i] - tmp
ss += dif
pq.get()
pq.put(a[i])
pq.put(a[i])
return ss
# Driver code
if __name__=="__main__":
a = [ 3, 1, 2, 1 ]
n = len(a)
print(DecreasingArray(a, n))
# This code is contributed by mathew