Skip to content
Problem 801 · LeetCode 801(opens in a new tab)

Minimum Swaps To Make Sequences Increasing

HardArrays

You are given two integer arrays of equal length, nums1 and nums2. In one move you pick an index i and exchange nums1[i] with nums2[i], so the two values at that position trade places.

Return the smallest number of moves that leaves both arrays strictly increasing, meaning every value is larger than the one before it. The input is always fixable.

Example 1

Input:  nums1 = [2, 5, 4, 9], nums2 = [1, 3, 6, 7]
Output: 1
Why:    swap index 2 to get [2, 5, 6, 9] and [1, 3, 4, 7]
2 more examples and the constraints

Example 2

Input:  nums1 = [1, 6, 3, 8, 5], nums2 = [2, 2, 7, 4, 9]
Output: 2
Why:    swap indices 1 and 3 to get [1, 2, 3, 4, 5]

Example 3

Input:  nums1 = [1, 2, 3], nums2 = [4, 5, 6]
Output: 0
Why:    both arrays already increase

Constraints

  • 2 ≤ length of each array ≤ 100,000
  • Both arrays have the same length
  • 0 ≤ every value ≤ 200,000
  • Some set of moves always makes both arrays strictly increasing

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Keep / swap DP tableO(n)
  2. Two rolling variablesO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100,000 positionsVerdict
Keep / swap DP tableO(n)
Clear; uses two arrays
Two rolling variablesO(n)
Best; same pass, no arrays

n = length of the arrays. Both read each index once; the second drops the O(n) memory.

Edge cases to test

  • Already increasing. [1, 2, 3] and [4, 5, 6] need 0 swaps.
  • Only one choice per step. When just one check passes, the other cell becomes infinity and must not be used.
  • Every index forced the same way. If all pairs are flipped, the answer may be to swap almost everything; the DP still picks the cheaper side.
  • Equal values in one array. Strictly increasing means 2 then 2 is not allowed, so use < and never ≤.
  • Two elements. The smallest input: one transition, answer 0 or 1.

Common mistakes

  • Using ≤ instead of <, which accepts equal neighbours.
  • Overwriting keep before computing swap, so swap reads the new keep.
  • Greedily swapping whenever a pair looks out of order: an early swap can force more later ones.

How to talk through it

  1. 1Clarify. Is a swap only between the same index of the two arrays? Strictly increasing? Always solvable?
  2. 2Name the brute force. 2ⁿ swap patterns; mention it and say why it explodes.
  3. 3Find the state. Only the choice at i − 1 limits the choice at i, so two states per index are enough.
  4. 4Code the two checks. Same side copies the previous state; crossed values allow flipping. Take minimums.
  5. 5Test out loud. Run Example 1 by hand, including the index where only the cross check passes.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?