[LeetCode] Minimize Maximum of Array

2439. Minimize Maximum of Array

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int minimizeArrayValue(vector<int>& nums) {
long long sum = 0, res = 0, cnt = 0;
for(auto n : nums) {
sum += n;
cnt += 1;
res = max(res, (sum + cnt - 1) / cnt);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/02/08/PS/LeetCode/minimize-maximum-of-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.