2439. Minimize Maximum of Array Medium

@problem@discussion
#Array#Binary Search#Dynamic Programming#Greedy#Prefix Sum



1/**
2 * [2439] Minimize Maximum of Array
3 *
4 * You are given a 0-indexed array nums comprising of n non-negative integers.
5 * In one operation, you must:
6 * 
7 * 	Choose an integer i such that 1 <= i < n and nums[i] > 0.
8 * 	Decrease nums[i] by 1.
9 * 	Increase nums[i - 1] by 1.
10 * 
11 * Return the minimum possible value of the maximum integer of nums after performing any number of operations.
12 *  
13 * <strong class="example">Example 1:
14 * 
15 * Input: nums = [3,7,1,6]
16 * Output: 5
17 * Explanation:
18 * One set of optimal operations is as follows:
19 * 1. Choose i = 1, and nums becomes [4,6,1,6].
20 * 2. Choose i = 3, and nums becomes [4,6,2,5].
21 * 3. Choose i = 1, and nums becomes [5,5,2,5].
22 * The maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5.
23 * Therefore, we return 5.
24 * 
25 * <strong class="example">Example 2:
26 * 
27 * Input: nums = [10,1]
28 * Output: 10
29 * Explanation:
30 * It is optimal to leave nums as is, and since 10 is the maximum value, we return 10.
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	n == nums.length
36 * 	2 <= n <= 10^5
37 * 	0 <= nums[i] <= 10^9
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/minimize-maximum-of-array/
43// discuss: https://leetcode.com/problems/minimize-maximum-of-array/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn minimize_array_value(nums: Vec<i32>) -> i32 {
49        0
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_2439() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.