Minimum Swaps To Make Sequences Increasing Problem
Description
LeetCode Problem 801.
You are given two integer arrays of the same length nums1 and nums2. In one operation, you are allowed to swap nums1[i] with nums2[i].
- For example, if nums1 = [1,2,3,8], and nums2 = [5,6,7,4], you can swap the element at i = 3 to obtain nums1 = [1,2,3,4] and nums2 = [5,6,7,8].
Return the minimum number of needed operations to make nums1 and nums2 strictly increasing. The test cases are generated so that the given input always makes it possible.
An array arr is strictly increasing if and only if arr[0] < arr[1] < arr[2] < ... < arr[arr.length - 1].
Example 1:
Input: nums1 = [1,3,5,4], nums2 = [1,2,3,7]
Output: 1
Explanation:
Swap nums1[3] and nums2[3]. Then the sequences are:
nums1 = [1, 3, 5, 7] and nums2 = [1, 2, 3, 4]
which are both strictly increasing.
Example 2:
Input: nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9]
Output: 1
Constraints:
- 2 <= nums1.length <= 10^5
- nums2.length == nums1.length
- 0 <= nums1[i], nums2[i] <= 2 * 10^5
Solution
Approach: Dynamic Programming with Two States
Each index is either swapped or not, so there are 2n choices. But whether index i is fine depends only on index i - 1, so it is enough to remember two numbers as we move from left to right:
keep: the fewest swaps that make the firsti + 1positions strictly increasing, with indexinot swapped.swap: the same, with indexiswapped.
Start with keep = 0 and swap = 1. For each i >= 1, look at the two ways the pairs at i - 1 and i can line up:
- If
nums1[i-1] < nums1[i]andnums2[i-1] < nums2[i], indeximay make the same choice asi - 1:newKeep = keepandnewSwap = swap + 1. - If
nums1[i-1] < nums2[i]andnums2[i-1] < nums1[i], indeximay make the opposite choice:newKeep = swapandnewSwap = keep + 1.
Both conditions can hold at once, so start newKeep and newSwap at infinity ("impossible") and take the minimum over the cases that apply. The guarantee that a solution exists means at least one case always applies. The answer is min(keep, swap) at the end.
The detail people get wrong is treating the two cases as either/or with else. When both hold, either choice at i - 1 works and we must keep the better one. In Example 2 ([0,3,5,8,9] and [2,1,4,6,9]), the first pairs (0, 2) and (3, 1) allow only case 2 at i = 1, while from i = 2 on both cases apply.
Complexity: O(n) time and O(1) space, where n is the length of nums1.
class Solution:
def minSwap(self, nums1: List[int], nums2: List[int]) -> int:
# keep / swap: fewest swaps so far when index i is left alone / swapped.
keep, swap = 0, 1
for i in range(1, len(nums1)):
new_keep = new_swap = float("inf")
# Both pairs already increase: index i makes the same choice as index i - 1.
if nums1[i - 1] < nums1[i] and nums2[i - 1] < nums2[i]:
new_keep = min(new_keep, keep)
new_swap = min(new_swap, swap + 1)
# The crossed pairs increase: index i makes the opposite choice.
if nums1[i - 1] < nums2[i] and nums2[i - 1] < nums1[i]:
new_keep = min(new_keep, swap)
new_swap = min(new_swap, keep + 1)
keep, swap = new_keep, new_swap
return min(keep, swap)