diff --git a/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README.md b/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README.md index 88d99c4cc6c2b..0045b4cf053c2 100644 --- a/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README.md +++ b/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README.md @@ -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); +} +``` + diff --git a/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README_EN.md b/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README_EN.md index 33d47cfbc5e53..05cfa68361a31 100644 --- a/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README_EN.md +++ b/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README_EN.md @@ -33,7 +33,7 @@ tags:
Input: nums1 = [1,3,5,4], nums2 = [1,2,3,7]
Output: 1
-Explanation:
+Explanation:
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.
@@ -61,7 +61,21 @@ which are both strictly increasing.
-### 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)$.
@@ -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);
+}
+```
+
diff --git a/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/Solution.ts b/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/Solution.ts
new file mode 100644
index 0000000000000..7f1453d8d43b9
--- /dev/null
+++ b/solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/Solution.ts
@@ -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);
+}