LeetCode 801: Minimum Swaps To Make Sequences Increasing Solution

Master LeetCode problem 801 (Minimum Swaps To Make Sequences Increasing), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

801. Minimum Swaps To Make Sequences Increasing

Problem Explanation

Explanation

To solve this problem, we can use dynamic programming. We define two arrays dpKeep and dpSwap to keep track of the minimum number of swaps needed to make nums1 and nums2 strictly increasing up to index i while keeping nums1[i] and nums2[i] unchanged or swapping them respectively. We update these arrays based on the previous indices.

At each index i, we have two options:

  1. If nums1[i] > nums1[i-1] and nums2[i] > nums2[i-1], we can keep the current elements as they are. In this case, the number of swaps needed is the same as the previous index for keeping both arrays.
  2. If nums1[i] > nums2[i-1] and nums2[i] > nums1[i-1], we can swap the current elements. In this case, the number of swaps needed is the minimum of swapping both arrays or keeping one array and swapping the other.

The final answer will be the minimum of the number of swaps needed at the last index for keeping both arrays and swapping them.

Time complexity: O(n) where n is the length of the input arrays Space complexity: O(1) as we only use constant extra space for the two arrays

Solution Code

class Solution {
    public int minSwap(int[] nums1, int[] nums2) {
        int n = nums1.length;
        int[] dpKeep = new int[n];
        int[] dpSwap = new int[n];
        
        // Base case
        dpSwap[0] = 1;
        
        for (int i = 1; i < n; i++) {
            dpKeep[i] = dpSwap[i] = n;
            if (nums1[i] > nums1[i-1] && nums2[i] > nums2[i-1]) {
                dpKeep[i] = dpKeep[i-1];
                dpSwap[i] = dpSwap[i-1] + 1;
            }
            if (nums1[i] > nums2[i-1] && nums2[i] > nums1[i-1]) {
                dpKeep[i] = Math.min(dpKeep[i], dpSwap[i-1]);
                dpSwap[i] = Math.min(dpSwap[i], dpKeep[i-1] + 1);
            }
        }
        
        return Math.min(dpKeep[n-1], dpSwap[n-1]);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 801 (Minimum Swaps To Make Sequences Increasing)?

This page provides optimized solutions for LeetCode problem 801 (Minimum Swaps To Make Sequences Increasing) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 801 (Minimum Swaps To Make Sequences Increasing)?

The time complexity for LeetCode 801 (Minimum Swaps To Make Sequences Increasing) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 801 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 801 in Java, C++, or Python.

Back to LeetCode Solutions