mediumArrayGreedyHash TableMonotonic StackStack 0 views

Minimum Operations to Convert All Elements to Zero

You are given an array nums of size n, consisting of non-negative integers.

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.

Minimum Operations to Convert All Elements to Zero diagram

Example 1

Input: nums = [0,2]

Output: 1

Example 2

Input: nums = [3,1,2,1]

Output: 3

Example 3

Input: nums = [1,2,1,2,1,2]

Output: 4

Constraints

  • 1 <= n == nums.length <= 10^5
  • 0 <= nums[i] <= 10^5

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.