- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two list of numbers called A and B, and they are of same length. Now consider we can perform an operation where we can swap numbers A[i] and B[i]. We have to find the number of operations required to make both lists strictly increasing.

So, if the input is like A = [2, 8, 7, 10] B = [2, 4, 9, 10], then the output will be 1, as we can swap 7 in A and 9 in B. Then the lists will be like A = [2, 8, 9, 10] and B = [2, 4, 7, 10] which are both strictly increasing lists.

To solve this, we will follow these steps:

- Define a function dp() . This will take i, prev_swapped
- if size of A is same as i, then
- return 0

- otherwise when i is same as 0, then
- return minimum of dp(i + 1, False) and 1 + dp(i + 1, True)

- otherwise,
- prev_A := A[i - 1]
- prev_B := B[i - 1]
- if prev_swapped is True, then
- swap prev_A and prev_B

- if A[i] <= prev_A or B[i] <= prev_B, then
- return 1 + dp(i + 1, True)

- otherwise,
- ans := dp(i + 1, False)
- if A[i] > prev_B and B[i] > prev_A, then
- ans := minimum of ans, 1 + dp(i + 1, True)

- return ans

- From the main method call the function dp(0, False) and return its value as result

Let us see the following implementation to get better understanding:

class Solution: def solve(self, A, B): def dp(i=0, prev_swapped=False): if len(A) == i: return 0 elif i == 0: return min(dp(i + 1), 1 + dp(i + 1, True)) else: prev_A = A[i - 1] prev_B = B[i - 1] if prev_swapped: prev_A, prev_B = prev_B, prev_A if A[i] <= prev_A or B[i] <= prev_B: return 1 + dp(i + 1, True) else: ans = dp(i + 1) if A[i] > prev_B and B[i] > prev_A: ans = min(ans, 1 + dp(i + 1, True)) return ans return dp() ob = Solution() A = [2, 8, 7, 10] B = [2, 4, 9, 10] print(ob.solve(A, B))

[2, 8, 7, 10], [2, 4, 9, 10]

1

- Related Questions & Answers
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum operations to make the array increasing using Python
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to find minimum number of operations to make string sorted in Python
- Minimum number of given operations required to make two strings equal using C++.
- Program to find minimum number of bricks required to make k towers of same height in Python
- Program to find length of contiguous strictly increasing sublist in Python
- Program to find minimum operations to make array equal using Python
- Program to find number of strictly increasing colorful candle sequences are there in Python
- Program to find number of given operations required to reach Target in Python
- Program to find minimum swaps required to make given anagram in python
- Program to find minimum remove required to make valid parentheses in Python
- Program to count number of minimum swaps required to make it palindrome in Python
- Program to find minimum number of deletions required from two ends to make list balanced in Python

Advertisements