Skip to content

feat: add solutions to lc problem: No.0801 #4081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,29 @@ func minSwap(nums1 []int, nums2 []int) int {
}
```

#### TypeScript

```ts
function minSwap(nums1: number[], nums2: number[]): number {
let [a, b] = [0, 1];
for (let i = 1; i < nums1.length; ++i) {
let x = a,
y = b;
if (nums1[i - 1] >= nums1[i] || nums2[i - 1] >= nums2[i]) {
a = y;
b = x + 1;
} else {
b = y + 1;
if (nums1[i - 1] < nums2[i] && nums2[i - 1] < nums1[i]) {
a = Math.min(a, y);
b = Math.min(b, x + 1);
}
}
}
return Math.min(a, b);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
<pre>
<strong>Input:</strong> nums1 = [1,3,5,4], nums2 = [1,2,3,7]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
<strong>Explanation:</strong>
Swap nums1[3] and nums2[3]. Then the sequences are:
nums1 = [1, 3, 5, 7] and nums2 = [1, 2, 3, 4]
which are both strictly increasing.
Expand Down Expand Up @@ -61,7 +61,21 @@ which are both strictly increasing.

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

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$.

When $i=0$, we have $a = 0$ and $b = 1$.

When $i \gt 0$, we first save the previous values of $a$ and $b$ in $x$ and $y$, and then discuss the following cases:

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$.

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)$.

Finally, return the smaller value between $a$ and $b$.

The time complexity is $O(n)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -151,6 +165,29 @@ func minSwap(nums1 []int, nums2 []int) int {
}
```

#### TypeScript

```ts
function minSwap(nums1: number[], nums2: number[]): number {
let [a, b] = [0, 1];
for (let i = 1; i < nums1.length; ++i) {
let x = a,
y = b;
if (nums1[i - 1] >= nums1[i] || nums2[i - 1] >= nums2[i]) {
a = y;
b = x + 1;
} else {
b = y + 1;
if (nums1[i - 1] < nums2[i] && nums2[i - 1] < nums1[i]) {
a = Math.min(a, y);
b = Math.min(b, x + 1);
}
}
}
return Math.min(a, b);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function minSwap(nums1: number[], nums2: number[]): number {
let [a, b] = [0, 1];
for (let i = 1; i < nums1.length; ++i) {
let x = a,
y = b;
if (nums1[i - 1] >= nums1[i] || nums2[i - 1] >= nums2[i]) {
a = y;
b = x + 1;
} else {
b = y + 1;
if (nums1[i - 1] < nums2[i] && nums2[i - 1] < nums1[i]) {
a = Math.min(a, y);
b = Math.min(b, x + 1);
}
}
}
return Math.min(a, b);
}