#2439

Minimize Maximum of Array

specialist · 945 · lc medium +32 · verified · 46.5% accepted · 2,581 likes · top 31%

Description

Given a 0-indexed array nums of n non-negative integers, you can perform this operation any number of times: choose an index i (where 1 <= i < n and nums[i] > 0), decrement nums[i] by 1, and increment nums[i - 1] by 1.

Return the minimum possible value of the maximum element in nums after 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.

Code

1
2
3