diff --git a/coin-change/jun0811.js b/coin-change/jun0811.js new file mode 100644 index 000000000..2253530e8 --- /dev/null +++ b/coin-change/jun0811.js @@ -0,0 +1,13 @@ +var coinChange = function(coins, amount) { + if(amount == 0) return 0 + + const dp = [0, ...new Array(amount).fill(amount+1)] + + for (const coin of coins) { + for (let i = coin; i <=amount; i++) { + dp[i] = Math.min(dp[i], dp[i-coin] + 1) + } + } + + return dp[amount] < amount+1 ? dp[amount] : -1 +}; diff --git a/find-minimum-in-rotated-sorted-array/jun0811.js b/find-minimum-in-rotated-sorted-array/jun0811.js new file mode 100644 index 000000000..145a30a77 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/jun0811.js @@ -0,0 +1,16 @@ +var findMin = function (nums) { + let left = 0, + 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; // 최솟값이 왼쪽에 있음 (mid 포함) + } + } + + return nums[left]; +}; diff --git a/maximum-depth-of-binary-tree/jun0811.js b/maximum-depth-of-binary-tree/jun0811.js new file mode 100644 index 000000000..15f00f438 --- /dev/null +++ b/maximum-depth-of-binary-tree/jun0811.js @@ -0,0 +1,22 @@ +var maxDepth = function (root) { + let res = 0; + + if (!root) return res; // root 자체가 없을 경우만 0 + + function check(node, depth) { + if (!node.left && !node.right) { + res = Math.max(res, depth); + return; + } + + if (node.left) { + check(node.left, depth + 1); + } + if (node.right) { + check(node.right, depth + 1); + } + } + + check(root, 1); // 루트부터 시작 + return res; +}; diff --git a/merge-two-sorted-lists/jun0811.js b/merge-two-sorted-lists/jun0811.js new file mode 100644 index 000000000..1a3ef292e --- /dev/null +++ b/merge-two-sorted-lists/jun0811.js @@ -0,0 +1,20 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ + +var mergeTwoLists = function (list1, list2) { + if (!list1) return list2; + else if (!list2) return list1; + + if (list1.val <= list2.val) { + list1.next = mergeTwoLists(list1.next, list2); + return list1; + } else { + list2.next = mergeTwoLists(list1, list2.next); + return list2; + } +}; diff --git a/word-search/jun0811.js b/word-search/jun0811.js new file mode 100644 index 000000000..ac31cba3d --- /dev/null +++ b/word-search/jun0811.js @@ -0,0 +1,58 @@ +/** + * @param {character[][]} board + * @param {string} word + * @return {boolean} + */ + +const directions = [ + [0, -1], + [1, 0], + [-1, 0], + [0, 1], +]; + +var exist = function (board, word) { + const cols = board[0].length; // 가로 (열 개수) + const rows = board.length; // 세로 (행 개수) + let res = false; + + for (let col = 0; col < cols; col++) { + for (let row = 0; row < rows; row++) { + if (board[row][col] != word[0]) continue; + + const visited = Array.from({ length: rows }, () => + Array(cols).fill(false) + ); + if (res) break; + dfs(row, col, board[row][col], visited); + } + } + + function check(row, col) { + if (!(row >= 0 && row < rows)) return false; + if (!(col >= 0 && col < cols)) return false; + return true; + } + + function dfs(row, col, str, visited) { + if (str == word) { + res = true; + return; + } + if (str.length >= word.length) return; + visited[row][col] = true; + + for (const direction of directions) { + const [d_r, d_c] = direction; + const newCol = col + d_c; + const newRow = row + d_r; + + if (check(newRow, newCol) && !visited[newRow][newCol]) { + dfs(newRow, newCol, str + board[newRow][newCol], visited); + } + } + visited[row][col] = false; + } + + return res; +};