Skip to content

Commit 7602e8c

Browse files
authored
Merge pull request #1817 from hyer0705/main
2 parents 42d4d23 + d878003 commit 7602e8c

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

coin-change/hyer0705.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function coinChange(coins: number[], amount: number): number {
2+
// dp[i] = i 금액을 만드는 데 필요한 최소 동전의 갯수...
3+
const dp: number[] = Array(amount + 1).fill(Infinity);
4+
dp[0] = 0;
5+
6+
for (let i = 1; i <= amount; i++) {
7+
let min = Infinity;
8+
for (const coin of coins) {
9+
if (i >= coin) {
10+
min = Math.min(dp[i - coin] + 1, min);
11+
}
12+
}
13+
dp[i] = min;
14+
}
15+
16+
return Number.isFinite(dp[amount]) ? dp[amount] : -1;
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function findMin(nums: number[]): number {
2+
const n = nums.length;
3+
4+
let l = 0;
5+
let r = n - 1;
6+
7+
while (l < r) {
8+
const mid = Math.floor((l + r) / 2);
9+
10+
if (nums[r] >= nums[mid]) {
11+
r = mid;
12+
} else if (nums[r] < nums[mid]) {
13+
l = mid + 1;
14+
}
15+
}
16+
17+
return nums[l];
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 maxDepth(root: TreeNode | null): number {
16+
if (!root) return 0;
17+
18+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
19+
}

merge-two-sorted-lists/hyer0705.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
14+
if (!list1 && !list2) return null;
15+
16+
if (!list1) return list2;
17+
if (!list2) return list1;
18+
19+
const dummyHead = new ListNode(-101);
20+
let tail = dummyHead;
21+
22+
let pointerOne: ListNode | null = list1;
23+
let pointerTwo: ListNode | null = list2;
24+
25+
while (pointerOne && pointerTwo) {
26+
if (pointerOne.val <= pointerTwo.val) {
27+
tail.next = pointerOne;
28+
pointerOne = pointerOne.next;
29+
} else {
30+
tail.next = pointerTwo;
31+
pointerTwo = pointerTwo.next;
32+
}
33+
34+
tail = tail.next;
35+
}
36+
37+
if (pointerOne) {
38+
tail.next = pointerOne;
39+
} else {
40+
tail.next = pointerTwo;
41+
}
42+
43+
return dummyHead.next;
44+
}

word-search/hyer0705.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function exist(board: string[][], word: string): boolean {
2+
const m = board.length;
3+
const n = board[0].length;
4+
5+
const visited: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false));
6+
7+
const isValidCoordinates = (index: number, x: number, y: number): boolean => {
8+
if (x >= 0 && x < m && y >= 0 && y < n && word[index] === board[x][y] && !visited[x][y]) return true;
9+
return false;
10+
};
11+
12+
const backtrack = (index: number, cx: number, cy: number): boolean => {
13+
if (index === word.length - 1 && board[cx][cy] === word[index]) return true;
14+
15+
if (board[cx][cy] !== word[index]) return false;
16+
17+
for (const [dx, dy] of [
18+
[0, 1],
19+
[0, -1],
20+
[1, 0],
21+
[-1, 0],
22+
]) {
23+
const [nx, ny] = [cx + dx, cy + dy];
24+
25+
if (isValidCoordinates(index + 1, nx, ny)) {
26+
visited[nx][ny] = true;
27+
const result = backtrack(index + 1, nx, ny);
28+
if (result) return true;
29+
visited[nx][ny] = false;
30+
}
31+
}
32+
33+
return false;
34+
};
35+
36+
for (let i = 0; i < m; i++) {
37+
for (let j = 0; j < n; j++) {
38+
if (board[i][j] === word[0]) {
39+
visited[i][j] = true;
40+
const result: boolean = backtrack(0, i, j);
41+
if (result) return true;
42+
visited[i][j] = false;
43+
}
44+
}
45+
}
46+
47+
return false;
48+
}

0 commit comments

Comments
 (0)