Program to find minimum number of operations required to make lists strictly Increasing in python

Given two lists A and B of equal length, we can swap elements at position i between the lists (swap A[i] with B[i]). The goal is to find the minimum number of swaps required to make both lists strictly increasing.

For example, with A = [2, 8, 7, 10] and B = [2, 4, 9, 10], we can swap elements at index 2 (swap 7 and 9) to get A = [2, 8, 9, 10] and B = [2, 4, 7, 10], both strictly increasing with just 1 swap.

Algorithm Approach

We use dynamic programming with recursion to explore two choices at each position:

  • Keep the current elements as they are
  • Swap the current elements

The recursive function dp(i, prev_swapped) returns the minimum swaps needed from position i onwards, where prev_swapped indicates if we swapped at the previous position.

Implementation

class Solution:
    def solve(self, A, B):
        def dp(i=0, prev_swapped=False):
            # Base case: reached end of lists
            if len(A) == i:
                return 0
            elif i == 0:
                # First position: try both options
                return min(dp(i + 1), 1 + dp(i + 1, True))
            else:
                prev_A = A[i - 1]
                prev_B = B[i - 1]
                
                # If previous elements were swapped, update references
                if prev_swapped:
                    prev_A, prev_B = prev_B, prev_A
                
                # If current elements violate increasing order, must swap
                if A[i] <= prev_A or B[i] <= prev_B:
                    return 1 + dp(i + 1, True)
                else:
                    # Try not swapping
                    ans = dp(i + 1)
                    # Try swapping if it maintains increasing order
                    if A[i] > prev_B and B[i] > prev_A:
                        ans = min(ans, 1 + dp(i + 1, True))
                    return ans
        
        return dp()

# Test the solution
ob = Solution()
A = [2, 8, 7, 10]
B = [2, 4, 9, 10]
print(ob.solve(A, B))
1

How It Works

The algorithm examines each position and determines whether to swap based on the strictly increasing constraint:

  1. Base case: When we reach the end of the lists, no more swaps are needed
  2. First position: We can either keep or swap, so we try both options
  3. Subsequent positions: We check if the current elements maintain the increasing order with the previous elements
  4. Forced swap: If keeping current elements would violate the increasing order, we must swap
  5. Optional swap: If both keeping and swapping maintain order, we choose the minimum

Example Walkthrough

For A = [2, 8, 7, 10] and B = [2, 4, 9, 10]:

  • Position 0: Both options (keep or swap) maintain order, explore both
  • Position 2: If we didn't swap at position 1, then 7 ? 8 violates increasing order in A, so we must swap
  • The optimal solution requires 1 swap at position 2

Conclusion

This dynamic programming solution efficiently finds the minimum swaps by exploring all valid combinations. The time complexity depends on the number of valid states, and the approach guarantees an optimal solution for making both lists strictly increasing.

Updated on: 2026-03-25T12:41:25+05:30

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements