3139. Minimum Cost to Equalize Array Hard

@problem@discussion
#Array#Greedy#Enumeration



1/**
2 * [3139] Minimum Cost to Equalize Array
3 *
4 * You are given an integer array nums and two integers cost1 and cost2. You are allowed to perform either of the following operations any number of times:
5 * 
6 * 	Choose an index i from nums and increase nums[i] by 1 for a cost of cost1.
7 * 	Choose two different indices i, j, from nums and increase nums[i] and nums[j] by 1 for a cost of cost2.
8 * 
9 * Return the minimum cost required to make all elements in the array equal. 
10 * Since the answer may be very large, return it modulo 10^9 + 7.
11 *  
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">nums = [4,1], cost1 = 5, cost2 = 2</span>
15 * Output: <span class="example-io">15</span>
16 * Explanation: 
17 * The following operations can be performed to make the values equal:
18 * 
19 * 	Increase nums[1] by 1 for a cost of 5. nums becomes [4,2].
20 * 	Increase nums[1] by 1 for a cost of 5. nums becomes [4,3].
21 * 	Increase nums[1] by 1 for a cost of 5. nums becomes [4,4].
22 * 
23 * The total cost is 15.
24 * </div>
25 * <strong class="example">Example 2:
26 * <div class="example-block">
27 * Input: <span class="example-io">nums = [2,3,3,3,5], cost1 = 2, cost2 = 1</span>
28 * Output: <span class="example-io">6</span>
29 * Explanation: 
30 * The following operations can be performed to make the values equal:
31 * 
32 * 	Increase nums[0] and nums[1] by 1 for a cost of 1. nums becomes [3,4,3,3,5].
33 * 	Increase nums[0] and nums[2] by 1 for a cost of 1. nums becomes [4,4,4,3,5].
34 * 	Increase nums[0] and nums[3] by 1 for a cost of 1. nums becomes [5,4,4,4,5].
35 * 	Increase nums[1] and nums[2] by 1 for a cost of 1. nums becomes [5,5,5,4,5].
36 * 	Increase nums[3] by 1 for a cost of 2. nums becomes [5,5,5,5,5].
37 * 
38 * The total cost is 6.
39 * </div>
40 * <strong class="example">Example 3:
41 * <div class="example-block">
42 * Input: <span class="example-io">nums = [3,5,3], cost1 = 1, cost2 = 3</span>
43 * Output: <span class="example-io">4</span>
44 * Explanation:
45 * The following operations can be performed to make the values equal:
46 * 
47 * 	Increase nums[0] by 1 for a cost of 1. nums becomes [4,5,3].
48 * 	Increase nums[0] by 1 for a cost of 1. nums becomes [5,5,3].
49 * 	Increase nums[2] by 1 for a cost of 1. nums becomes [5,5,4].
50 * 	Increase nums[2] by 1 for a cost of 1. nums becomes [5,5,5].
51 * 
52 * The total cost is 4.
53 * </div>
54 *  
55 * Constraints:
56 * 
57 * 	1 <= nums.length <= 10^5
58 * 	1 <= nums[i] <= 10^6
59 * 	1 <= cost1 <= 10^6
60 * 	1 <= cost2 <= 10^6
61 * 
62 */
63pub struct Solution {}
64
65// problem: https://leetcode.com/problems/minimum-cost-to-equalize-array/
66// discuss: https://leetcode.com/problems/minimum-cost-to-equalize-array/discuss/?currentPage=1&orderBy=most_votes&query=
67
68// submission codes start here
69
70impl Solution {
71    pub fn min_cost_to_equalize_array(nums: Vec<i32>, cost1: i32, cost2: i32) -> i32 {
72        0
73    }
74}
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_3139() {
84    }
85}
86


Back
© 2025 bowen.ge All Rights Reserved.