3542. Minimum Operations to Convert All Elements to Zero Medium
1/**
2 * [3542] Minimum Operations to Convert All Elements to Zero
3 *
4 * You are given an array nums of size n, consisting of non-negative integers. Your task is to apply some (possibly zero) operations on the array so that all elements become 0.
5 * In one operation, you can select a <span data-keyword="subarray">subarray</span> [i, j] (where 0 <= i <= j < n) and set all occurrences of the minimum non-negative integer in that subarray to 0.
6 * Return the minimum number of operations required to make all elements in the array 0.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [0,2]</span>
11 * Output: <span class="example-io">1</span>
12 * Explanation:
13 *
14 * Select the subarray [1,1] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0].
15 * Thus, the minimum number of operations required is 1.
16 * </div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">nums = [3,1,2,1]</span>
20 * Output: <span class="example-io">3</span>
21 * Explanation:
22 *
23 * Select subarray [1,3] (which is [1,2,1]), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in [3,0,2,0].
24 * Select subarray [2,2] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [3,0,0,0].
25 * Select subarray [0,0] (which is [3]), where the minimum non-negative integer is 3. Setting all occurrences of 3 to 0 results in [0,0,0,0].
26 * Thus, the minimum number of operations required is 3.
27 * </div>
28 * <strong class="example">Example 3:
29 * <div class="example-block">
30 * Input: <span class="example-io">nums = [1,2,1,2,1,2]</span>
31 * Output: <span class="example-io">4</span>
32 * Explanation:
33 *
34 * Select subarray [0,5] (which is [1,2,1,2,1,2]), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in [0,2,0,2,0,2].
35 * Select subarray [1,1] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0,0,2,0,2].
36 * Select subarray [3,3] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0,0,0,0,2].
37 * Select subarray [5,5] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0,0,0,0,0].
38 * Thus, the minimum number of operations required is 4.
39 * </div>
40 *
41 * Constraints:
42 *
43 * 1 <= n == nums.length <= 10^5
44 * 0 <= nums[i] <= 10^5
45 *
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/minimum-operations-to-convert-all-elements-to-zero/
50// discuss: https://leetcode.com/problems/minimum-operations-to-convert-all-elements-to-zero/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55 pub fn min_operations(nums: Vec<i32>) -> i32 {
56 0
57 }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_3542() {
68 }
69}
70Back
© 2026 bowen.ge All Rights Reserved.