2439. Minimize Maximum of Array LeetCode Solution

In this guide, you will get 2439. Minimize Maximum of Array LeetCode Solution with the best time and space complexity. The solution to Minimize Maximum of Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimize Maximum of Array solution in C++
  4. Minimize Maximum of Array solution in Java
  5. Minimize Maximum of Array solution in Python
  6. Additional Resources
2439. Minimize Maximum of Array LeetCode Solution image

Problem Statement of 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 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

See also  2917. Find the K-or of an Array LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2439. Minimize Maximum of Array LeetCode Solution in C++

class Solution {
 public:
  int minimizeArrayValue(vector<int>& nums) {
    long ans = 0;
    long prefix = 0;

    for (int i = 0; i < nums.size(); ++i) {
      prefix += nums[i];
      const long prefixAvg = ceil(prefix / static_cast<double>(i + 1));
      ans = max(ans, prefixAvg);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2439. Minimize Maximum of Array LeetCode Solution in Java

class Solution {
  public int minimizeArrayValue(int[] nums) {
    long ans = 0;
    long prefix = 0;

    for (int i = 0; i < nums.length; ++i) {
      prefix += nums[i];
      final long prefixAvg = (long) Math.ceil(prefix / (double) (i + 1));
      ans = Math.max(ans, prefixAvg);
    }

    return (int) ans;
  }
}
// code provided by PROGIEZ

2439. Minimize Maximum of Array LeetCode Solution in Python

class Solution:
  def minimizeArrayValue(self, nums: list[int]) -> int:
    ans = 0
    prefix = 0

    for i, num in enumerate(nums):
      prefix += num
      prefixAvg = math.ceil(prefix / (i + 1))
      ans = max(ans, prefixAvg)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.