Medium
You are given a 0-indexed array nums
comprising of n
non-negative integers.
In one operation, you must:
i
such that 1 <= i < n
and nums[i] > 0
.nums[i]
by 1.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:
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
class Solution {
fun minimizeArrayValue(nums: IntArray): Int {
var max: Long = 0
var sum: Long = 0
for (i in nums.indices) {
sum += nums[i].toLong()
max = Math.max(max, (sum + i) / (i + 1))
}
return max.toInt()
}
}