2439. Minimize Maximum of Array
https://leetcode.com/problems/minimize-maximum-of-array
Python
Greedy
- 加與減要同時發生,所以整個array的總和不變
nums[0]
無法被減,因此答案只會大於或等於nums[0]
from math import ceil
class Solution:
def minimizeArrayValue(self, nums: List[int]) -> int:
total = nums[0]
n = len(nums)
ans, i = 0, 1
while i < n:
till_now = ceil(total/i)
total += nums[i]
i += 1
current = ceil(total/i)
ans = max(till_now, current, ans)
return ans