diff --git a/coin-change/seueooo.js b/coin-change/seueooo.js new file mode 100644 index 0000000000..44672f3157 --- /dev/null +++ b/coin-change/seueooo.js @@ -0,0 +1,26 @@ +/** + * bfs + * 시간 복잡도: O(amount * n) + * 공간 복잡도: O(amount) + * @param {number[]} coins + * @param {number} amount + * @return {number} + */ +var coinChange = function (coins, amount) { + let q = []; + let visited = new Set(); + q.push([0, 0]); + while (q.length) { + const [sum, count] = q.shift(); + if (sum === amount) return count; + if (visited.has(sum)) continue; + visited.add(sum); + for (let i = 0; i < coins.length; i++) { + const nextSum = sum + coins[i]; + if (nextSum <= amount) { + q.push([nextSum, count + 1]); + } + } + } + return -1; +}; diff --git a/find-minimum-in-rotated-sorted-array/seueooo.js b/find-minimum-in-rotated-sorted-array/seueooo.js new file mode 100644 index 0000000000..00e8bbee0f --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/seueooo.js @@ -0,0 +1,22 @@ +/** +이분 탐색 +시간 복잡도 : O(log n) +공간 복잡도 : O(1) + * @param {number[]} nums + * @return {number} + */ +var findMin = function (nums) { + let left = 0; + let right = nums.length - 1; + + while (left < right) { + let mid = Math.floor((left + right) / 2); + if (nums[mid] > nums[right]) { + left = mid + 1; + } else { + right = mid; + } + } + + return nums[left]; +}; diff --git a/maximum-depth-of-binary-tree/seueooo.js b/maximum-depth-of-binary-tree/seueooo.js new file mode 100644 index 0000000000..1ccb8fa258 --- /dev/null +++ b/maximum-depth-of-binary-tree/seueooo.js @@ -0,0 +1,17 @@ +/** +재귀 + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var maxDepth = function (root) { + if (!root) return 0; + return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); +}; diff --git a/merge-two-sorted-lists/seueooo.js b/merge-two-sorted-lists/seueooo.js new file mode 100644 index 0000000000..7388f82f66 --- /dev/null +++ b/merge-two-sorted-lists/seueooo.js @@ -0,0 +1,31 @@ +/** + * 풀이 + * 더 작은 노드를 머리로 두고, 그 next에 나머지를 병합한 결과를 연결. + * 최종적으로 머리를 반환 + * + * 시간복잡도 - O(n + m) : n, m은 각각 list1, list2의 길이 + * 공간복잡도 - O(n + m) : 재귀 호출 스택 + * + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + */ +var mergeTwoLists = function (list1, list2) { + if (!list1) return list2; + 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/seueooo.js b/word-search/seueooo.js new file mode 100644 index 0000000000..9be589bf93 --- /dev/null +++ b/word-search/seueooo.js @@ -0,0 +1,56 @@ +/** +현재 경로를 하나 선택해서 끝까지 진행하고, 실패하면 방문 표시를 되돌려 다른 경로를 시도 +bfs, 백트래킹 + * @param {character[][]} board + * @param {string} word + * @return {boolean} + */ +var exist = function (board, word) { + const rows = board.length; + const cols = board[0].length; + + const dx = [-1, 1, 0, 0]; + const dy = [0, 0, -1, 1]; + + function dfs(x, y, index) { + if (board[y][x] !== word[index]) { + return false; + } + + if (index === word.length - 1) { + return true; + } + + // 방문 처리 + const original = board[y][x]; + board[y][x] = "#"; + + for (let i = 0; i < 4; i++) { + const nx = x + dx[i]; + const ny = y + dy[i]; + + if ( + nx >= 0 && + nx < cols && + ny >= 0 && + ny < rows && + board[ny][nx] !== "#" + ) { + if (dfs(nx, ny, index + 1)) { + board[y][x] = original; + return true; + } + } + } + + board[y][x] = original; + return false; + } + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (dfs(j, i, 0)) return true; + } + } + return false; +};