Skip to content

Commit 757f7b8

Browse files
committed
https://leetcode.cn/problems/linked-list-components/
1 parent 2cbaf55 commit 757f7b8

File tree

5 files changed

+61
-34
lines changed

5 files changed

+61
-34
lines changed

6eUYwP/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import pathSum from "../path-sum-iii/index.ts";
2-
3-
export default pathSum;
1+
import pathSum from "../path-sum-iii/index.ts";
2+
3+
export default pathSum;

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,8 @@ https://leetcode.cn/problems/delete-node-in-a-linked-list/
13161316

13171317
https://leetcode.cn/problems/4ueAj6/
13181318

1319+
https://leetcode.cn/problems/linked-list-components/
1320+
13191321
https://leetcode.cn/problems/insert-into-a-sorted-circular-linked-list/
13201322

13211323
https://leetcode.cn/problems/insert-delete-getrandom-o1/

linked-list-components/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ListNode } from "../reverse-linked-list/ListNode.ts";
2+
3+
export default function numComponents(
4+
head: ListNode | null,
5+
nums: number[],
6+
): number {
7+
const numsSet = new Set<number>();
8+
for (const num of nums) {
9+
numsSet.add(num);
10+
}
11+
let inSet = false;
12+
let res = 0;
13+
while (head) {
14+
if (numsSet.has(head.val)) {
15+
if (!inSet) {
16+
inSet = true;
17+
res++;
18+
}
19+
} else {
20+
inSet = false;
21+
}
22+
head = head.next;
23+
}
24+
return res;
25+
}
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
export default function minSwap(nums1: number[], nums2: number[]): number {
2-
const n = nums1.length;
3-
let a = 0,
4-
b = 1;
5-
for (let i = 1; i < n; i++) {
6-
const at = a,
7-
bt = b;
8-
a = b = n;
9-
if (nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1]) {
10-
a = Math.min(a, at);
11-
b = Math.min(b, bt + 1);
12-
}
13-
if (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1]) {
14-
a = Math.min(a, bt);
15-
b = Math.min(b, at + 1);
16-
}
17-
}
18-
return Math.min(a, b);
19-
}
1+
export default function minSwap(nums1: number[], nums2: number[]): number {
2+
const n = nums1.length;
3+
let a = 0,
4+
b = 1;
5+
for (let i = 1; i < n; i++) {
6+
const at = a,
7+
bt = b;
8+
a = b = n;
9+
if (nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1]) {
10+
a = Math.min(a, at);
11+
b = Math.min(b, bt + 1);
12+
}
13+
if (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1]) {
14+
a = Math.min(a, bt);
15+
b = Math.min(b, at + 1);
16+
}
17+
}
18+
return Math.min(a, b);
19+
}

three-in-one-lcci/test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2-
import TripleInOne from "./index.ts";
3-
4-
Deno.test("three-in-one-lcci", () => {
5-
const tio = new TripleInOne(1);
6-
tio.push(0, 1);
7-
tio.push(0, 2);
8-
assertEquals(1, tio.pop(0));
9-
assertEquals(-1, tio.pop(0));
10-
assertEquals(-1, tio.pop(0));
11-
assertEquals(true, tio.isEmpty(0));
12-
});
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import TripleInOne from "./index.ts";
3+
4+
Deno.test("three-in-one-lcci", () => {
5+
const tio = new TripleInOne(1);
6+
tio.push(0, 1);
7+
tio.push(0, 2);
8+
assertEquals(1, tio.pop(0));
9+
assertEquals(-1, tio.pop(0));
10+
assertEquals(-1, tio.pop(0));
11+
assertEquals(true, tio.isEmpty(0));
12+
});

0 commit comments

Comments
 (0)