2448. Minimum Cost to Make Array Equal Hard
1/**
2 * [2448] Minimum Cost to Make Array Equal
3 *
4 * You are given two 0-indexed arrays nums and cost consisting each of n positive integers.
5 * You can do the following operation any number of times:
6 *
7 * Increase or decrease any element of the array nums by 1.
8 *
9 * The cost of doing one operation on the i^th element is cost[i].
10 * Return the minimum total cost such that all the elements of the array nums become equal.
11 *
12 * <strong class="example">Example 1:
13 *
14 * Input: nums = [1,3,5,2], cost = [2,3,1,14]
15 * Output: 8
16 * Explanation: We can make all the elements equal to 2 in the following way:
17 * - Increase the 0^th element one time. The cost is 2.
18 * - Decrease the 1^<span style="font-size: 10.8333px;">st</span> element one time. The cost is 3.
19 * - Decrease the 2^nd element three times. The cost is 1 + 1 + 1 = 3.
20 * The total cost is 2 + 3 + 3 = 8.
21 * It can be shown that we cannot make the array equal with a smaller cost.
22 *
23 * <strong class="example">Example 2:
24 *
25 * Input: nums = [2,2,2,2,2], cost = [4,2,8,1,3]
26 * Output: 0
27 * Explanation: All the elements are already equal, so no operations are needed.
28 *
29 *
30 * Constraints:
31 *
32 * n == nums.length == cost.length
33 * 1 <= n <= 10^5
34 * 1 <= nums[i], cost[i] <= 10^6
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/minimum-cost-to-make-array-equal/
40// discuss: https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn min_cost(nums: Vec<i32>, cost: Vec<i32>) -> i64 {
46
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_2448() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.