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
17 changes: 17 additions & 0 deletions coin-change/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function coinChange(coins: number[], amount: number): number {
// dp[i] = i 금액을 만드는 데 필요한 최소 동전의 갯수...
const dp: number[] = Array(amount + 1).fill(Infinity);
dp[0] = 0;

for (let i = 1; i <= amount; i++) {
let min = Infinity;
for (const coin of coins) {
if (i >= coin) {
min = Math.min(dp[i - coin] + 1, min);
}
}
dp[i] = min;
}

return Number.isFinite(dp[amount]) ? dp[amount] : -1;
}
18 changes: 18 additions & 0 deletions find-minimum-in-rotated-sorted-array/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function findMin(nums: number[]): number {
const n = nums.length;

let l = 0;
let r = n - 1;

while (l < r) {
const mid = Math.floor((l + r) / 2);

if (nums[r] >= nums[mid]) {
r = mid;
} else if (nums[r] < nums[mid]) {
l = mid + 1;
}
}

return nums[l];
}
19 changes: 19 additions & 0 deletions maximum-depth-of-binary-tree/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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 maxDepth(root: TreeNode | null): number {
if (!root) return 0;

return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
44 changes: 44 additions & 0 deletions merge-two-sorted-lists/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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 mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
if (!list1 && !list2) return null;

if (!list1) return list2;
if (!list2) return list1;

const dummyHead = new ListNode(-101);
let tail = dummyHead;

let pointerOne: ListNode | null = list1;
let pointerTwo: ListNode | null = list2;

while (pointerOne && pointerTwo) {
if (pointerOne.val <= pointerTwo.val) {
tail.next = pointerOne;
pointerOne = pointerOne.next;
} else {
tail.next = pointerTwo;
pointerTwo = pointerTwo.next;
}

tail = tail.next;
}

if (pointerOne) {
tail.next = pointerOne;
} else {
tail.next = pointerTwo;
}

return dummyHead.next;
}
48 changes: 48 additions & 0 deletions word-search/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function exist(board: string[][], word: string): boolean {
const m = board.length;
const n = board[0].length;

const visited: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false));

const isValidCoordinates = (index: number, x: number, y: number): boolean => {
if (x >= 0 && x < m && y >= 0 && y < n && word[index] === board[x][y] && !visited[x][y]) return true;
return false;
};

const backtrack = (index: number, cx: number, cy: number): boolean => {
if (index === word.length - 1 && board[cx][cy] === word[index]) return true;

if (board[cx][cy] !== word[index]) return false;

for (const [dx, dy] of [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
]) {
const [nx, ny] = [cx + dx, cy + dy];

if (isValidCoordinates(index + 1, nx, ny)) {
visited[nx][ny] = true;
const result = backtrack(index + 1, nx, ny);
if (result) return true;
visited[nx][ny] = false;
}
}

return false;
};

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (board[i][j] === word[0]) {
visited[i][j] = true;
const result: boolean = backtrack(0, i, j);
if (result) return true;
visited[i][j] = false;
}
}
}

return false;
}