Talentd
FleetCode

2439. Minimize Maximum of Array

Medium46.4% acceptance5 companies

You are given a 0-indexed array nums comprising of n non-negative integers.

In one operation, you must:

  • Choose an integer i such that 1 <= i < n and nums[i] > 0.
  • Decrease nums[i] by 1.
  • Increase nums[i - 1] by 1.

Return the minimum possible value of the maximum integer of nums after performing any number of operations.

Example 1:

Input: nums = [3,7,1,6]
Output: 5
Explanation:
One set of optimal operations is as follows:
1. Choose i = 1, and nums becomes [4,6,1,6].
2. Choose i = 3, and nums becomes [4,6,2,5].
3. Choose i = 1, and nums becomes [5,5,2,5].
The maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5.
Therefore, we return 5.

Example 2:

Input: nums = [10,1]
Output: 10
Explanation:
It is optimal to leave nums as is, and since 10 is the maximum value, we return 10.

Constraints:

  • n == nums.length
  • 2 <= n <= 105
  • 0 <= nums[i] <= 109

Hints open one at a time

Hint 1
Try a binary search approach.
Hint 2
Perform a binary search over the minimum value that can be achieved for the maximum number of the array.
Hint 3
In each binary search iteration, iterate through the array backwards, greedily decreasing the current element until it is within the limit.
9:50
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minimizeArrayValue(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[3,7,1,6]
Input
[10,1]
Your code is saved on this device.Solution