#801
Minimum Swaps To Make Sequences Increasing
master · 1665 · lc hard +32 · verified · 41.3% accepted · 2,941 likes · top 22%
Description
You are given two integer arrays of equal length, nums1 and nums2. In one operation you may swap nums1[i] with nums2[i].
- For example, given nums1 = [1,2,3,8] and nums2 = [5,6,7,4], swapping at index i = 3 yields nums1 = [1,2,3,4] and nums2 = [5,6,7,8].
Return the minimum number of such swap operations needed to make both nums1 and nums2 strictly increasing. The input always has a valid solution.
An array arr is strictly increasing 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
Code
1
2
3