2208. Minimum Operations to Halve Array Sum Medium

@problem@discussion
#Array#Greedy#Heap (Priority Queue)



1/**
2 * [2208] Minimum Operations to Halve Array Sum
3 *
4 * You are given an array nums of positive integers. In one operation, you can choose any number from nums and reduce it to exactly half the number. (Note that you may choose this reduced number in future operations.)
5 * Return the minimum number of operations to reduce the sum of nums by at least half.
6 *  
7 * Example 1:
8 * 
9 * Input: nums = [5,19,8,1]
10 * Output: 3
11 * Explanation: The initial sum of nums is equal to 5 + 19 + 8 + 1 = 33.
12 * The following is one of the ways to reduce the sum by at least half:
13 * Pick the number 19 and reduce it to 9.5.
14 * Pick the number 9.5 and reduce it to 4.75.
15 * Pick the number 8 and reduce it to 4.
16 * The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75. 
17 * The sum of nums has been reduced by 33 - 14.75 = 18.25, which is at least half of the initial sum, 18.25 >= 33/2 = 16.5.
18 * Overall, 3 operations were used so we return 3.
19 * It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
20 * 
21 * Example 2:
22 * 
23 * Input: nums = [3,8,20]
24 * Output: 3
25 * Explanation: The initial sum of nums is equal to 3 + 8 + 20 = 31.
26 * The following is one of the ways to reduce the sum by at least half:
27 * Pick the number 20 and reduce it to 10.
28 * Pick the number 10 and reduce it to 5.
29 * Pick the number 3 and reduce it to 1.5.
30 * The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5. 
31 * The sum of nums has been reduced by 31 - 14.5 = 16.5, which is at least half of the initial sum, 16.5 >= 31/2 = 16.5.
32 * Overall, 3 operations were used so we return 3.
33 * It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	1 <= nums.length <= 10^5
39 * 	1 <= nums[i] <= 10^7
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/minimum-operations-to-halve-array-sum/
45// discuss: https://leetcode.com/problems/minimum-operations-to-halve-array-sum/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn halve_array(nums: Vec<i32>) -> i32 {
51        0
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_2208() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.