3469. Find Minimum Cost to Remove Array Elements Medium
1/**
2 * [3469] Find Minimum Cost to Remove Array Elements
3 *
4 * You are given an integer array nums. Your task is to remove all elements from the array by performing one of the following operations at each step until nums is empty:
5 *
6 * Choose any two elements from the first three elements of nums and remove them. The cost of this operation is the maximum of the two elements removed.
7 * If fewer than three elements remain in nums, remove all the remaining elements in a single operation. The cost of this operation is the maximum of the remaining elements.
8 *
9 * Return the minimum cost required to remove all the elements.
10 *
11 * <strong class="example">Example 1:
12 * <div class="example-block">
13 * Input: <span class="example-io">nums = [6,2,8,4]</span>
14 * Output: <span class="example-io">12</span>
15 * Explanation:
16 * Initially, nums = [6, 2, 8, 4].
17 *
18 * In the first operation, remove nums[0] = 6 and nums[2] = 8 with a cost of max(6, 8) = 8. Now, nums = [2, 4].
19 * In the second operation, remove the remaining elements with a cost of max(2, 4) = 4.
20 *
21 * The cost to remove all elements is 8 + 4 = 12. This is the minimum cost to remove all elements in nums. Hence, the output is 12.
22 * </div>
23 * <strong class="example">Example 2:
24 * <div class="example-block">
25 * Input: <span class="example-io">nums = [2,1,3,3]</span>
26 * Output: <span class="example-io">5</span>
27 * Explanation:
28 * Initially, nums = [2, 1, 3, 3].
29 *
30 * In the first operation, remove nums[0] = 2 and nums[1] = 1 with a cost of max(2, 1) = 2. Now, nums = [3, 3].
31 * In the second operation remove the remaining elements with a cost of max(3, 3) = 3.
32 *
33 * The cost to remove all elements is 2 + 3 = 5. This is the minimum cost to remove all elements in nums. Hence, the output is 5.
34 * </div>
35 *
36 * Constraints:
37 *
38 * 1 <= nums.length <= 1000
39 * 1 <= nums[i] <= 10^6
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/find-minimum-cost-to-remove-array-elements/
45// discuss: https://leetcode.com/problems/find-minimum-cost-to-remove-array-elements/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn min_cost(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_3469() {
63 }
64}
65Back
© 2026 bowen.ge All Rights Reserved.