Skip to content
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
18 changes: 18 additions & 0 deletions non-overlapping-intervals/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[1] - b[1]);

let removedCount = 0;
let prevEnd = intervals[0][1];

for (let i = 1; i < intervals.length; i++) {
const [start, end] = intervals[i];

if (prevEnd > start) {
removedCount++;
} else {
prevEnd = end;
}
}

return removedCount;
}
36 changes: 36 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export class Solution {
/**
* @param n: the number of vertices
* @param edges: the edges of undirected graph
* @return: the number of connected components
Comment on lines +3 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

세심하게 변수에 대한 설명을 적어놓으신 게 인상 깊습니다.

*/
countComponents(n: number, edges: number[][]): number {
const parent = Array.from({ length: n }, (_, i) => i);
let components = n;

const find = (i: number): number => {
if (parent[i] === i) {
return i;
}

parent[i] = find(parent[i]);
return parent[i];
};

const union = (a: number, b: number): void => {
const rootA = find(a);
const rootB = find(b);

if (rootA !== rootB) {
parent[rootA] = rootB;
components--;
}
};

for (const [a, b] of edges) {
union(a, b);
}

return components;
}
}
34 changes: 34 additions & 0 deletions remove-nth-node-from-end-of-list/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
if (!head) return null;

let current = head;
let nodeLen = 0;
while (current.next) {
current = current.next;
nodeLen++;
}

if (nodeLen - n < 0) return head.next;

current = head;
let count = 0;
while (count < nodeLen - n) {
current = current.next;
count++;
}
current.next = current.next.next;

return head;
}
21 changes: 21 additions & 0 deletions same-tree/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
if (!p && !q) return true;
if (!p || !q) return false;
Comment on lines +16 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

타입스크립트 문법은 신기하네요. 첫번째는 둘 다 null인지 확인하는 부분, 두번째는 한쪽만 null인지 확인하는 if조건문 맞죠?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 맞습니다~

if (p.val !== q.val) return false;

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
54 changes: 54 additions & 0 deletions serialize-and-deserialize-binary-tree/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

/*
* Encodes a tree to a single string.
*/
function serialize(root: TreeNode | null): string {
if (!root) return "null";

let result = "" + root.val;

result += "," + serialize(root.left);
result += "," + serialize(root.right);

return result;
}

/*
* Decodes your encoded data to tree.
*/
function deserialize(data: string): TreeNode | null {
const values = data.split(",");
let index = 0;

const buildTree = () => {
const val = values[index];
index++;

if (val === "null") return null;

const node = new TreeNode(Number(val));
node.left = buildTree();
node.right = buildTree();
return node;
};

return buildTree();
}

/**
* Your functions will be called as such:
* deserialize(serialize(root));
*/