Tap here to switch tabs
Problem
Submissions

Problem: Minimum Unlocked Indices to Sort Nums

Try to solve the Minimum Unlocked Indices to Sort Nums problem.

Statement

You are given an integer array nums where each element is 11, 22, or 33. You are also given a binary array locked of the same length, where locked[i] == 1 indicates that index i is locked and locked[i] == 0 indicates that index i is unlocked.

You can perform the following operation any number of times:

  • Choose an unlocked index i such that 0≤i<0 \leq i < nums.length −1- 1 and nums[i] −- nums[i + 1] ==1== 1, and swap nums[i] with nums[i + 1].

You may unlock any number of locked indices by changing their corresponding value in locked from 11 to 00.

Return the minimum number of indices that must be unlocked so that nums can be sorted in nondecreasing order. If it is impossible to sort nums, return −1-1.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤3\leq 3

  • locked.length ==== nums.length

  • 0≤0 \leq locked[i] ≤1\leq 1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Unlocked Indices to Sort Nums

Try to solve the Minimum Unlocked Indices to Sort Nums problem.

Statement

You are given an integer array nums where each element is 11, 22, or 33. You are also given a binary array locked of the same length, where locked[i] == 1 indicates that index i is locked and locked[i] == 0 indicates that index i is unlocked.

You can perform the following operation any number of times:

  • Choose an unlocked index i such that 0≤i<0 \leq i < nums.length −1- 1 and nums[i] −- nums[i + 1] ==1== 1, and swap nums[i] with nums[i + 1].

You may unlock any number of locked indices by changing their corresponding value in locked from 11 to 00.

Return the minimum number of indices that must be unlocked so that nums can be sorted in nondecreasing order. If it is impossible to sort nums, return −1-1.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤3\leq 3

  • locked.length ==== nums.length

  • 0≤0 \leq locked[i] ≤1\leq 1