819. Minimum Swaps To Make Sequences Increasing
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of swaps between nums1[i] and nums2[i] to make both sequences strictly increasing. Since the decision at index i (to swap or not) depends on the state of the arrays at index i-1, this suggests a dynamic programming approach. A greedy choice at each step might not yield a globally optimal solution.
Solution
Minimum Swaps To Make Sequences Increasing in Python
class Solution:
def minSwap(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
# keep: min swaps to make prefixes [0...i] increasing, WITHOUT swapping at i
# swap: min swaps to make prefixes [0...i] increasing, WITH a swap at i
keep = 0
swap = 1
for i in range(1, n):
# Temporary variables to hold the new values for the current index i
# We use these to avoid overwriting keep and swap before they are fully used for calculation
next_keep = float('inf')
next_swap = float('inf')
# Condition 1: The elements are in order without any swaps at index i relative to i-1.
# This means nums1[i] > nums1[i-1] and nums2[i] > nums2[i-1].
if nums1[i] > nums1[i-1] and nums2[i] > nums2[i-1]:
# To not swap at i, we must not have swapped at i-1.
next_keep = min(next_keep, keep)
# To swap at i, we must have swapped at i-1.
next_swap = min(next_swap, swap + 1)
# Condition 2: The elements are in order if we cross-compare.
# This means nums1[i] > nums2[i-1] and nums2[i] > nums1[i-1].
if nums1[i] > nums2[i-1] and nums2[i] > nums1[i-1]:
# To not swap at i, we must have swapped at i-1.
next_keep = min(next_keep, swap)
# To swap at i, we must not have swapped at i-1.
next_swap = min(next_swap, keep + 1)
# Update states for the next iteration
keep = next_keep
swap = next_swap
# The result is the minimum of the two states at the last element
return min(keep, swap)
Minimum Swaps To Make Sequences Increasing in Java
class Solution {
public int minSwap(int[] nums1, int[] nums2) {
int n = nums1.length;
// keep: min swaps to make prefixes [0...i] increasing, WITHOUT swapping at i
// swap: min swaps to make prefixes [0...i] increasing, WITH a swap at i
int keep = 0;
int swap = 1;
for (int i = 1; i < n; i++) {
// Temporary variables to hold the new values for the current index i
// We use these to avoid overwriting keep and swap before they are fully used for calculation
int nextKeep = Integer.MAX_VALUE;
int nextSwap = Integer.MAX_VALUE;
// Condition 1: If we don't swap at i, can we maintain the increasing order?
// This is possible if nums1[i] > nums1[i-1] AND nums2[i] > nums2[i-1]
boolean noSwapPossible = (nums1[i] > nums1[i-1] && nums2[i] > nums2[i-1]);
if (noSwapPossible) {
// To not swap at i, we must not have swapped at i-1.
nextKeep = Math.min(nextKeep, keep);
// To swap at i, we must have swapped at i-1.
nextSwap = Math.min(nextSwap, swap + 1);
}
// Condition 2: If we swap at i, can we maintain the increasing order?
// This is possible if nums1[i] > nums2[i-1] AND nums2[i] > nums1[i-1]
boolean swapPossible = (nums1[i] > nums2[i-1] && nums2[i] > nums1[i-1]);
if (swapPossible) {
// To not swap at i, we must have swapped at i-1.
nextKeep = Math.min(nextKeep, swap);
// To swap at i, we must not have swapped at i-1.
nextSwap = Math.min(nextSwap, keep + 1);
}
// Update the states for the next iteration
keep = nextKeep;
swap = nextSwap;
}
// The result is the minimum of the two states at the last element
return Math.min(keep, swap);
}
}
Minimum Swaps To Make Sequences Increasing in C++
class Solution {
public:
int minSwap(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
// keep: min swaps to make prefixes [0...i] increasing, WITHOUT swapping at i
// swap_val: min swaps to make prefixes [0...i] increasing, WITH a swap at i
int keep = 0;
int swap_val = 1;
for (int i = 1; i < n; ++i) {
// Temporary variables to hold the new values for the current index i
// We use these to avoid overwriting keep and swap_val before they are fully used for calculation
int next_keep = INT_MAX;
int next_swap = INT_MAX;
// Condition 1: If we don't swap at i, can we maintain the increasing order?
// This is possible if nums1[i] > nums1[i-1] AND nums2[i] > nums2[i-1]
bool no_swap_possible = (nums1[i] > nums1[i-1] && nums2[i] > nums2[i-1]);
if (no_swap_possible) {
// To not swap at i, we must not have swapped at i-1.
next_keep = min(next_keep, keep);
// To swap at i, we must have swapped at i-1.
next_swap = min(next_swap, swap_val + 1);
}
// Condition 2: If we swap at i, can we maintain the increasing order?
// This is possible if nums1[i] > nums2[i-1] AND nums2[i] > nums1[i-1]
bool swap_possible = (nums1[i] > nums2[i-1] && nums2[i] > nums1[i-1]);
if (swap_possible) {
// To not swap at i, we must have swapped at i-1.
next_keep = min(next_keep, swap_val);
// To swap at i, we must not have swapped at i-1.
next_swap = min(next_swap, keep + 1);
}
// Update the states for the next iteration
keep = next_keep;
swap_val = next_swap;
}
// The result is the minimum of the two states at the last element
return min(keep, swap_val);
}
};
Minimum Swaps To Make Sequences Increasing in TypeScript
function minSwap(nums1: number[], nums2: number[]): number {
const n = nums1.length;
// keep: min swaps to make prefixes [0...i] increasing, WITHOUT swapping at i
// swap: min swaps to make prefixes [0...i] increasing, WITH a swap at i
let keep = 0;
let swap = 1;
for (let i = 1; i < n; i++) {
// Temporary variables to hold the new values for the current index i
// We use these to avoid overwriting keep and swap before they are fully used for calculation
let nextKeep = Number.MAX_SAFE_INTEGER;
let nextSwap = Number.MAX_SAFE_INTEGER;
// Condition 1: If we don't swap at i, can we maintain the increasing order?
// This is possible if nums1[i] > nums1[i-1] AND nums2[i] > nums2[i-1]
const noSwapPossible = nums1[i] > nums1[i-1] && nums2[i] > nums2[i-1];
if (noSwapPossible) {
// To not swap at i, we must not have swapped at i-1.
nextKeep = Math.min(nextKeep, keep);
// To swap at i, we must have swapped at i-1.
nextSwap = Math.min(nextSwap, swap + 1);
}
// Condition 2: If we swap at i, can we maintain the increasing order?
// This is possible if nums1[i] > nums2[i-1] AND nums2[i] > nums1[i-1]
const swapPossible = nums1[i] > nums2[i-1] && nums2[i] > nums1[i-1];
if (swapPossible) {
// To not swap at i, we must have swapped at i-1.
nextKeep = Math.min(nextKeep, swap);
// To swap at i, we must not have swapped at i-1.
nextSwap = Math.min(nextSwap, keep + 1);
}
// Update the states for the next iteration
keep = nextKeep;
swap = nextSwap;
}
// The result is the minimum of the two states at the last element
return Math.min(keep, swap);
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.