Minimum Operations to Sort With Locked Indices Problem
Description
LeetCode Problem 4099.
You are given an integer array nums of length n, where nums is a permutation of the integers in the range [1, n].
You are also given a binary array locked of length n and an integer k.
locked[i] = 1means indexiis locked.locked[i] = 0means indexiis unlocked.
You may perform the following operations in any order:
- Swap: Choose an index
isuch that both indicesiandi + 1are currently unlocked, and swapnums[i]andnums[i + 1]. - Unlock: Choose an index
isuch thatlocked[i] = 1and permanently unlock it by settinglocked[i] = 0.
You may perform any number of swap operations but at most k unlock operations.
Return the minimum total number of operations required to sort nums in strictly increasing order. If it is impossible, return -1.
Example 1:
Input: nums = [2,1,3,4], locked = [0,1,0,1], k = 1
Output: 2
Explanation:
- Unlock index 1. The array locked becomes [0,0,0,1].
- Swap nums[0] and nums[1]. The array nums becomes [1,2,3,4].
The total number of operations is 1 + 1 = 2.
Example 2:
Input: nums = [3,1,2], locked = [1,0,1], k = 2
Output: 4
Explanation:
- Unlock indices 0 and 2. All indices are now unlocked.
- Swap indices 0 and 1. The array nums becomes [1,3,2].
- Swap indices 1 and 2. The array nums becomes [1,2,3].
The total number of operations is 2 + 2 = 4.
Example 3:
Input: nums = [2,3,1], locked = [1,0,0], k = 0
Output: -1
Explanation:
Index 0 is locked, so its value cannot be moved. Since k = 0, no indices can be unlocked. Therefore, sorting the array is impossible.
Constraints:
1 <= n == nums.length == locked.length <= 20001 <= nums[i] <= n0 <= locked[i] <= 1numsis a permutation of the integers in the range[1, n].0 <= k <= n
Can you solve this in O(nlog(n))?
Solution
Approach: Inversions plus Required Unlocks
The total cost splits into swaps and unlocks, which can be computed separately.
- Swaps. An adjacent swap fixes at most one inversion, so the minimum number of swaps is the number of inversions of
nums. Count them with a Fenwick tree by scanning from the right and querying how many smaller values are already inserted. - Unlocks. Scan from the left keeping the prefix maximum
mx. Whenevermx == i + 1, the prefix[j..i]since the previous cut holds exactly the values it should, so no element crosses its boundary. If the segment has length 1 the element is already in place and needs no unlock. In a longer segment, every position must take part in some swap, so every locked index in it must be unlocked.
If the number of required unlocks exceeds k, return -1; otherwise the answer is unlocks plus inversions.
Complexity: O(n log n) time and O(n) space.
class BinaryIndexedTree:
__slots__ = "n", "c"
def __init__(self, n: int):
self.n = n
self.c = [0] * (n + 1)
def update(self, x: int, delta: int) -> None:
while x <= self.n:
self.c[x] += delta
x += x & -x
def query(self, x: int) -> int:
s = 0
while x:
s += self.c[x]
x -= x & -x
return s
class Solution:
def minOperations(self, nums: List[int], locked: List[int], k: int) -> int:
n = len(nums)
bit = BinaryIndexedTree(n)
inv = 0
for x in reversed(nums):
inv += bit.query(x - 1)
bit.update(x, 1)
cnt = mx = j = 0
for i, x in enumerate(nums):
mx = max(mx, x)
if mx == i + 1:
if i > j:
cnt += sum(locked[j : i + 1])
j = i + 1
return -1 if cnt > k else cnt + inv