All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2401_2500.s2439_minimize_maximum_of_array.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g2401_2500.s2439_minimize_maximum_of_array;

// #Medium #Array #Dynamic_Programming #Greedy #Binary_Search #Prefix_Sum
// #2022_12_13_Time_10_ms_(90.25%)_Space_83_MB_(30.03%)

/**
 * 2439 - Minimize Maximum of Array\.
 *
 * Medium
 *
 * 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_.
 *
 * **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
**/
public class Solution {
    public int minimizeArrayValue(int[] nums) {
        long max = 0;
        long sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            max = Math.max(max, (sum + i) / (i + 1));
        }
        return (int) max;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy