[
array
heap
binary search
]
BinarySearch 0500 Optimal Decrement
Problem statement
https://binarysearch.com/problems/Optimal-Decrement/
Solution
Just use heap to extract biggest element k
times and put decreased element.
Complexity
It is O(k log n)
for time and O(n)
for space.
Code
class Solution:
def solve(self, nums, k):
h = [-x for x in nums]
heapify(h)
for _ in range(k):
el = heappop(h)
heappush(h, el + 1)
return -h[0]
Remark
Or we can use binary search here to check if we can reach maximum value equal to x
.