Minimum Swaps To Make Sequences Increasing

HardDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given two integer arrays nums1 and nums2 of the same length. In one operation you may choose an index i and swap nums1[i] with nums2[i]. Return the minimum number of operations needed to make **both** arrays strictly increasing, meaning arr[0] < arr[1] < ... < arr[n-1] for each of them. The input is guaranteed to allow a solution. Aim for O(n) time and O(1) extra space.

Examples

Input: nums1 = [1,3,5,4], nums2 = [1,2,3,7]
Output: 1
Swapping index 3 gives [1,3,5,7] and [1,2,3,4], both strictly increasing.
Input: nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9]
Output: 1
Swapping index 1 gives [0,1,5,8,9] and [2,3,4,6,9].

Constraints

  • 2 <= nums1.length == nums2.length <= 10^5
  • 0 <= nums1[i], nums2[i] <= 2 * 10^5
  • A valid sequence of swaps always exists

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems