3542. Minimum Operations to Convert All Elements to Zero
Description
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.
In one operation, you can select a subarray [i, j] (where 0 <= i <= j < n) and set all occurrences of the minimum non-negative integer in that subarray to 0.
Return the minimum number of operations required to make all elements in the array 0.
Example 1:
Input: nums = [0,2]
Output: 1
Explanation:
- 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]. - Thus, the minimum number of operations required is 1.
Example 2:
Input: nums = [3,1,2,1]
Output: 3
Explanation:
- 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]. - 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]. - 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]. - Thus, the minimum number of operations required is 3.
Example 3:
Input: nums = [1,2,1,2,1,2]
Output: 4
Explanation:
- 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]. - 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]. - 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]. - 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]. - Thus, the minimum number of operations required is 4.
Constraints:
1 <= n == nums.length <= 1050 <= nums[i] <= 105
Solutions
Solution 1: Monotonic Stack
According to the problem description, we should first convert the smallest numbers to \(0\), then the second smallest numbers to \(0\), and so on. During this process, if two numbers are separated by smaller numbers, they require an additional operation to become \(0\).
We can maintain a monotonically increasing stack \(\textit{stk}\) from bottom to top, and traverse each number \(\textit{x}\) in the array \(\textit{nums}\):
- When the top element of the stack is greater than \(\textit{x}\), it means \(\textit{x}\) separates the top element. We need to pop the top element and increment the answer by \(1\), continuing until the top element is not greater than \(\textit{x}\).
- If \(\textit{x}\) is not \(0\), and the stack is empty or the top element is not equal to \(\textit{x}\), then push \(\textit{x}\) onto the stack.
After traversal, the remaining elements in the stack all require an additional operation to become \(0\), so we add the size of the stack to the answer.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the length of the array \(\textit{nums}\).
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |