Skip to content

[hoyeongkwak] WEEK 4 Solutions #1366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2025
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
20 changes: 20 additions & 0 deletions coin-change/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function coinChange(coins: number[], amount: number): number {
/*
O(n)
O(n)
*/
if (amount === 0) return 0

const dp = new Array(amount + 1).fill(Infinity)
dp[0] = 0
for(let coin of coins) {
for (let idx = coin; idx <= amount; idx++) {
dp[idx] = Math.min(dp[idx], dp[idx - coin] + 1)
}
}
if (dp[amount] === Infinity) {
return -1
} else {
return dp[amount]
}
};
24 changes: 24 additions & 0 deletions find-minimum-in-rotated-sorted-array/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function findMin(nums: number[]): number {
/*
space complexity: 1
time complexity : nlogn
*/
// return nums.sort((a, b) => a - b)[0]

/*
space complexity: 1
time complexity : logn
*/
let left = 0
let right = nums.length - 1

while (left < right) {
const mid = left + Math.floor((right - left) / 2)
if (nums[mid] > nums[right]) {
left = mid + 1
} else {
right = mid
}
}
return nums[left]
};
25 changes: 25 additions & 0 deletions maximum-depth-of-binary-tree/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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)
* }
* }
*/

/*
time complexity : O(n)
space complexity : O(logn)
*/

function maxDepth(root: TreeNode | null): number {
if (root == null) return 0
let maxLeft = maxDepth(root.left)
let maxRight = maxDepth(root.right)
return Math.max(maxLeft, maxRight) + 1
};
30 changes: 30 additions & 0 deletions merge-two-sorted-lists/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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)
* }
* }
*/

/*
time complexity : O(m + n)
space complexity : O(m + n)
*/

function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
if (list1 == null) return list2
if (list2 == null) return list1

if (list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2)
return list1
} else {
list2.next = mergeTwoLists(list1, list2.next)
return list2
}

};
40 changes: 40 additions & 0 deletions word-search/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function exist(board: string[][], word: string): boolean {
/*
O(m * n)
O(m * n)
*/
const rowsLen = board.length
const colsLen = board[0].length
const direction = [[0, 1], [1, 0], [0, -1], [-1, 0]]
const visited = Array(rowsLen).fill(0).map(() => Array(colsLen).fill(false))
const dfs = (row: number, col: number, idx: number): boolean => {
if (idx === word.length)
return true
if (row < 0 || row >= rowsLen || col < 0 ||
col >= colsLen || board[row][col] !== word[idx] || visited[row][col])
return false
visited[row][col] = true
if (idx === word.length - 1) return true
for (const [dx, dy] of direction) {
const newRow = row + dx
const newCol = col + dy

if (newRow >= 0 && newRow < rowsLen && newCol >= 0 && newCol < colsLen && !visited[newRow][newCol] && board[newRow][newCol] === word[idx + 1]) {
if (dfs(newRow, newCol, idx + 1)) {
return true
}
}
}
visited[row][col] = false
return false
}

for (let r = 0; r < rowsLen; r++) {
for(let c = 0; c < colsLen; c++) {
if (board[r][c] === word[0] && dfs(r, c, 0)) {
return true
}
}
}
return false
};