Skip to content

Commit a2f003c

Browse files
authored
Merge pull request #1937 from hyer0705/main
2 parents f6a5a58 + bc998d2 commit a2f003c

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function eraseOverlapIntervals(intervals: number[][]): number {
2+
intervals.sort((a, b) => a[1] - b[1]);
3+
4+
let removedCount = 0;
5+
let prevEnd = intervals[0][1];
6+
7+
for (let i = 1; i < intervals.length; i++) {
8+
const [start, end] = intervals[i];
9+
10+
if (prevEnd > start) {
11+
removedCount++;
12+
} else {
13+
prevEnd = end;
14+
}
15+
}
16+
17+
return removedCount;
18+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export class Solution {
2+
/**
3+
* @param n: the number of vertices
4+
* @param edges: the edges of undirected graph
5+
* @return: the number of connected components
6+
*/
7+
countComponents(n: number, edges: number[][]): number {
8+
const parent = Array.from({ length: n }, (_, i) => i);
9+
let components = n;
10+
11+
const find = (i: number): number => {
12+
if (parent[i] === i) {
13+
return i;
14+
}
15+
16+
parent[i] = find(parent[i]);
17+
return parent[i];
18+
};
19+
20+
const union = (a: number, b: number): void => {
21+
const rootA = find(a);
22+
const rootB = find(b);
23+
24+
if (rootA !== rootB) {
25+
parent[rootA] = rootB;
26+
components--;
27+
}
28+
};
29+
30+
for (const [a, b] of edges) {
31+
union(a, b);
32+
}
33+
34+
return components;
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
14+
if (!head) return null;
15+
16+
let current = head;
17+
let nodeLen = 0;
18+
while (current.next) {
19+
current = current.next;
20+
nodeLen++;
21+
}
22+
23+
if (nodeLen - n < 0) return head.next;
24+
25+
current = head;
26+
let count = 0;
27+
while (count < nodeLen - n) {
28+
current = current.next;
29+
count++;
30+
}
31+
current.next = current.next.next;
32+
33+
return head;
34+
}

same-tree/hyer0705.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
16+
if (!p && !q) return true;
17+
if (!p || !q) return false;
18+
if (p.val !== q.val) return false;
19+
20+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
21+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
/*
16+
* Encodes a tree to a single string.
17+
*/
18+
function serialize(root: TreeNode | null): string {
19+
if (!root) return "null";
20+
21+
let result = "" + root.val;
22+
23+
result += "," + serialize(root.left);
24+
result += "," + serialize(root.right);
25+
26+
return result;
27+
}
28+
29+
/*
30+
* Decodes your encoded data to tree.
31+
*/
32+
function deserialize(data: string): TreeNode | null {
33+
const values = data.split(",");
34+
let index = 0;
35+
36+
const buildTree = () => {
37+
const val = values[index];
38+
index++;
39+
40+
if (val === "null") return null;
41+
42+
const node = new TreeNode(Number(val));
43+
node.left = buildTree();
44+
node.right = buildTree();
45+
return node;
46+
};
47+
48+
return buildTree();
49+
}
50+
51+
/**
52+
* Your functions will be called as such:
53+
* deserialize(serialize(root));
54+
*/

0 commit comments

Comments
 (0)