Skip to content

Commit cdf5b7c

Browse files
authored
Create 801-minimum-swaps-to-make-sequences-increasing.js
1 parent b813706 commit cdf5b7c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} A
3+
* @param {number[]} B
4+
* @return {number}
5+
*/
6+
const minSwap = function(A, B) {
7+
let swapRecord = 1, fixRecord = 0;
8+
for (let i = 1; i < A.length; i++) {
9+
if (A[i - 1] >= B[i] || B[i - 1] >= A[i]) {
10+
// In this case, the ith manipulation should be same as the i-1th manipulation
11+
// fixRecord = fixRecord;
12+
swapRecord++;
13+
} else if (A[i - 1] >= A[i] || B[i - 1] >= B[i]) {
14+
// In this case, the ith manipulation should be the opposite of the i-1th manipulation
15+
let temp = swapRecord;
16+
swapRecord = fixRecord + 1;
17+
fixRecord = temp;
18+
} else {
19+
// Either swap or fix is OK. Let's keep the minimum one
20+
let min = Math.min(swapRecord, fixRecord);
21+
swapRecord = min + 1;
22+
fixRecord = min;
23+
}
24+
}
25+
return Math.min(swapRecord, fixRecord);
26+
};

0 commit comments

Comments
 (0)