Skip to content

Commit 092db9e

Browse files
author
Partho Biswas
committed
801_Minimum_Swaps_To_Make_Sequences_Increasing
1 parent 8d5470b commit 092db9e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
565565
|35| [115. Distinct Subsequences](https://tinyurl.com/gudjxts) | [Python](https://tinyurl.com/wu6rdaw/115_Distinct_Subsequences.py)| **[educative.io](https://tinyurl.com/yx6s5c4m)** | Hard | TODO: Check again |
566566
|36| **[97. Interleaving String](https://tinyurl.com/qmp8yn5)** | [Python](https://tinyurl.com/wu6rdaw/97_Interleaving_String.py)| **[educative.io](https://tinyurl.com/uzbogsf)**, [Vid 1](https://tinyurl.com/wlmpvjo) | Hard | TODO: Check again. Very difficult and tricky to understand |
567567
|37| [1048. Longest String Chain](https://tinyurl.com/uvb5v6s) | [Python](https://tinyurl.com/wu6rdaw/1048_Longest_String_Chain.py)| [Art 1](https://tinyurl.com/tcslm9l) | Medium | Modified LIS |
568+
|38| **[801. Minimum Swaps To Make Sequences Increasing](https://tinyurl.com/rvtcyvb)** | [Python](https://tinyurl.com/wu6rdaw/801_Minimum_Swaps_To_Make_Sequences_Increasing.py)| [Art 1](https://tinyurl.com/tzx7wpv), [Art 2](https://tinyurl.com/ugybcmz) | Medium | TODO: Check again. Very analytical and tricky to come up with |
568569

569570

570571
</p>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Source: https://tinyurl.com/tzx7wpv
3+
class Solution(object):
4+
def minSwap(self, A, B):
5+
"""
6+
:type A: List[int]
7+
:type B: List[int]
8+
:rtype: int
9+
"""
10+
arrLen = len(A)
11+
swaps, noSwaps = [arrLen] * arrLen, [arrLen] * arrLen
12+
swaps[0], noSwaps[0] = 1, 0
13+
for i in range(1, arrLen):
14+
currentNumA, prevNumA = A[i], A[i - 1]
15+
currentNumB, prevNumB = B[i], B[i - 1]
16+
if currentNumA > prevNumA and currentNumB > prevNumB:
17+
noSwaps[i] = noSwaps[i - 1]
18+
swaps[i] = swaps[i - 1] + 1
19+
if currentNumB > prevNumA and currentNumA > prevNumB:
20+
noSwaps[i] = min(noSwaps[i], swaps[i - 1]) # If we do ot make the swap in this case
21+
swaps[i] = min(swaps[i], noSwaps[i - 1] + 1) # If we make the swap in this case
22+
return min(swaps[-1], noSwaps[-1])

0 commit comments

Comments
 (0)