Skip to content

Commit f82785c

Browse files
authored
feat: add solutions to lc problem: No.0801 (#4081)
No.0801.Minimum Swaps To Make Sequences Increasing
1 parent da48265 commit f82785c

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,29 @@ func minSwap(nums1 []int, nums2 []int) int {
172172
}
173173
```
174174

175+
#### TypeScript
176+
177+
```ts
178+
function minSwap(nums1: number[], nums2: number[]): number {
179+
let [a, b] = [0, 1];
180+
for (let i = 1; i < nums1.length; ++i) {
181+
let x = a,
182+
y = b;
183+
if (nums1[i - 1] >= nums1[i] || nums2[i - 1] >= nums2[i]) {
184+
a = y;
185+
b = x + 1;
186+
} else {
187+
b = y + 1;
188+
if (nums1[i - 1] < nums2[i] && nums2[i - 1] < nums1[i]) {
189+
a = Math.min(a, y);
190+
b = Math.min(b, x + 1);
191+
}
192+
}
193+
}
194+
return Math.min(a, b);
195+
}
196+
```
197+
175198
<!-- tabs:end -->
176199

177200
<!-- solution:end -->

solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README_EN.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tags:
3333
<pre>
3434
<strong>Input:</strong> nums1 = [1,3,5,4], nums2 = [1,2,3,7]
3535
<strong>Output:</strong> 1
36-
<strong>Explanation:</strong>
36+
<strong>Explanation:</strong>
3737
Swap nums1[3] and nums2[3]. Then the sequences are:
3838
nums1 = [1, 3, 5, 7] and nums2 = [1, 2, 3, 4]
3939
which are both strictly increasing.
@@ -61,7 +61,21 @@ which are both strictly increasing.
6161

6262
<!-- solution:start -->
6363

64-
### Solution 1
64+
### Solution 1: Dynamic Programming
65+
66+
Define $a$ and $b$ to represent the minimum number of swaps needed to make the element sequences strictly increasing up to index $[0..i]$, with the $i$-th element not swapped and swapped, respectively. The index starts from $0$.
67+
68+
When $i=0$, we have $a = 0$ and $b = 1$.
69+
70+
When $i \gt 0$, we first save the previous values of $a$ and $b$ in $x$ and $y$, and then discuss the following cases:
71+
72+
If $nums1[i - 1] \ge nums1[i]$ or $nums2[i - 1] \ge nums2[i]$, to make both sequences strictly increasing, the relative positions of the elements at indices $i-1$ and $i$ must change. That is, if the previous position was swapped, then the current position should not be swapped, so $a = y$; if the previous position was not swapped, then the current position must be swapped, so $b = x + 1$.
73+
74+
Otherwise, the relative positions of the elements at indices $i-1$ and $i$ do not need to change, so $b = y + 1$. Additionally, if $nums1[i - 1] \lt nums2[i]$ and $nums2[i - 1] \lt nums1[i]$, the relative positions of the elements at indices $i-1$ and $i$ can change, so $a$ and $b$ can take the smaller values, thus $a = \min(a, y)$ and $b = \min(b, x + 1)$.
75+
76+
Finally, return the smaller value between $a$ and $b$.
77+
78+
The time complexity is $O(n)$, and the space complexity is $O(1)$.
6579

6680
<!-- tabs:start -->
6781

@@ -151,6 +165,29 @@ func minSwap(nums1 []int, nums2 []int) int {
151165
}
152166
```
153167

168+
#### TypeScript
169+
170+
```ts
171+
function minSwap(nums1: number[], nums2: number[]): number {
172+
let [a, b] = [0, 1];
173+
for (let i = 1; i < nums1.length; ++i) {
174+
let x = a,
175+
y = b;
176+
if (nums1[i - 1] >= nums1[i] || nums2[i - 1] >= nums2[i]) {
177+
a = y;
178+
b = x + 1;
179+
} else {
180+
b = y + 1;
181+
if (nums1[i - 1] < nums2[i] && nums2[i - 1] < nums1[i]) {
182+
a = Math.min(a, y);
183+
b = Math.min(b, x + 1);
184+
}
185+
}
186+
}
187+
return Math.min(a, b);
188+
}
189+
```
190+
154191
<!-- tabs:end -->
155192

156193
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function minSwap(nums1: number[], nums2: number[]): number {
2+
let [a, b] = [0, 1];
3+
for (let i = 1; i < nums1.length; ++i) {
4+
let x = a,
5+
y = b;
6+
if (nums1[i - 1] >= nums1[i] || nums2[i - 1] >= nums2[i]) {
7+
a = y;
8+
b = x + 1;
9+
} else {
10+
b = y + 1;
11+
if (nums1[i - 1] < nums2[i] && nums2[i - 1] < nums1[i]) {
12+
a = Math.min(a, y);
13+
b = Math.min(b, x + 1);
14+
}
15+
}
16+
}
17+
return Math.min(a, b);
18+
}

0 commit comments

Comments
 (0)