Skip to content

[HoonDongKang] Week 04 Solutions #1345

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 8 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
32 changes: 32 additions & 0 deletions find-minimum-in-rotated-sorted-array/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* [Problem]: [153] Find Minimum in Rotated Sorted Array
*
* (https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/)
*/

function findMin(nums: number[]): number {
// 실제 Submit은 되나, 문제의 의도와 다름
function ggomsuFunc(nums: number[]): number {
return Math.min(...nums);
}

//시간복잡도 O(logn)
//공간복잡도 O(1)
function binarySearchFunc(nums: number[]): number {
let left = 0;
let right = nums.length - 1;

while (left < right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}

return nums[left];
}

return binarySearchFunc(nums);
}
46 changes: 46 additions & 0 deletions maximum-depth-of-binary-tree/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* [Problem]: [104] Maximum Depth of Binary Tree
*
* (https://leetcode.com/problems/maximum-depth-of-binary-tree/description/)
*/

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 {
//시간복잡도 O(n)
//공간복잡도 O(n)
function stackFunc(root: TreeNode | null): number {
if (!root) return 0;
let max = 0;
const stack: [TreeNode, number][] = [[root, 1]];

while (stack.length > 0) {
const [node, depth] = stack.pop()!;
max = Math.max(max, depth);

if (node.left) stack.push([node.left, depth + 1]);
if (node.right) stack.push([node.right, depth + 1]);
}

return max;
}

//시간복잡도 O(n)
//공간복잡도 O(n)
function recursiveFunc(root: TreeNode | null): number {
if (!root) return 0;

return 1 + Math.max(recursiveFunc(root.left), recursiveFunc(root.right));
}

return recursiveFunc(root);
}
56 changes: 56 additions & 0 deletions merge-two-sorted-lists/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* [Problem]: [21] Merge Two Sorted Lists
*
* (https://leetcode.com/problems/merge-two-sorted-lists/description/)
*/

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 {
//시간복잡도 O(m+n)
//공간복잡도 O(1)
function loopFunc(list1: ListNode | null, list2: ListNode | null): ListNode | null {
let current: ListNode = new ListNode();
let dummy = current;

while (list1 !== null && list2 !== null) {
if (list1.val < list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}

current = current.next;
}

current.next = list1 !== null ? list1 : list2;

return dummy.next;
}

//시간복잡도 O(m+n)
//공간복잡도 O(m+n)
function recursionFunc(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 = recursionFunc(list1.next, list2);
return list1;
} else {
list2.next = recursionFunc(list1, list2.next);
return list2;
}
}

return recursionFunc(list1, list2);
}
83 changes: 83 additions & 0 deletions word-search/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* [Problem]: [79] Word Search
*
* (https://leetcode.com/problems/word-search/description/)
*/

function exist(board: string[][], word: string): boolean {
//시간복잡도 O(m * n * 4^w)
//공간복잡도 O(w)
function dfsChangingBoard(board: string[][], word: string): boolean {
const rows = board.length;
const cols = board[0].length;

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (dfs(i, j, 0)) return true;
}
}

return false;

function dfs(i: number, j: number, k: number): boolean {
if (k === word.length) return true;

if (i < 0 || j < 0) return false;
if (i >= rows || j >= cols) return false;
if (board[i][j] !== word[k]) return false;

const temp = board[i][j];
board[i][j] = "";

const result =
dfs(i + 1, j, k + 1) ||
dfs(i - 1, j, k + 1) ||
dfs(i, j + 1, k + 1) ||
dfs(i, j - 1, k + 1);

board[i][j] = temp;
return result;
}
}

//시간복잡도 O(m * n * 4^w)
//공간복잡도 O(w)
function dfsMaintainingBoard(board: string[][], word: string): boolean {
const rows = board.length;
const cols = board[0].length;

// array보다 Set<string>이 더 효율적
// Set.has: O(1) / Array.includes: O(n)
const passed = new Set<string>();

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (dfs(i, j, 0)) return true;
}
}

return false;

function dfs(i: number, j: number, k: number): boolean {
if (k === word.length) return true;

if (i < 0 || j < 0) return false;
if (i >= rows || j >= cols) return false;
if (board[i][j] !== word[k]) return false;
if (passed.has(`${i},${j}`)) return false;

passed.add(`${i},${j}`);

const result =
dfs(i + 1, j, k + 1) ||
dfs(i - 1, j, k + 1) ||
dfs(i, j + 1, k + 1) ||
dfs(i, j - 1, k + 1);

passed.delete(`${i},${j}`);
return result;
}
}

return dfsMaintainingBoard(board, word);
}