Coding
2439. Minimize Maximum of Array
On this page
⭐⭐⭐
題目敘述
You are given a 0-indexed array nums comprising of n non-negative integers.
In one operation, you must:
- Choose an integer
isuch that1 <= i < nandnums[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:
- Choose i = 1, and nums becomes [4,6,1,6].
- Choose i = 3, and nums becomes [4,6,2,5].
- 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.
解題思路
在題目敘述中我們可以知道,nums 這個陣列無法將前面較大的數值向後移,只能將後面的數值向前移動。
因此我們可以藉由 prefix sum 來計算到目前的總值,除以現在的陣列數目,得到平均值利用高斯取頂(ceiling function) $\Rightarrow$ 得到當前 subArray 數目的最大值
並與前面的最大值比較:
- 如果比較大 $\Rightarrow$ 就代表後面的值可以再向前移動。
- 如果比較小 $\Rightarrow$ 就表示雖然平均最大值可能有更小的,但是前面的值不可以往後移,因此不採用。
Algorithm
- Initialize
ans = 0andprefixSum = 0. - Iterate over
nums, for each indexi:- Update the prefix sum as
prefixSum += nums[i]. - Check the maximum value we can obtain by averaging
prefixSumintoi + 1evenly using ceiling division. - Take the larger one from
ansand the result from the previous integer division.
- Update the prefix sum as
- Return
ans
Solution
class Solution {
public int minimizeArrayValue(int[] nums) {
long prefixSum = 0;
int ans = 0;
for(int i = 0; i < nums.length; i++){
prefixSum += nums[i];
ans = Math.max(ans, (int)Math.ceil(prefixSum * 1.0 / (i + 1)));
}
return ans;
}
}
單字
片語 & 搭配詞
comprising of
包括