diff --git a/combination-sum/seungseung88.js b/combination-sum/seungseung88.js new file mode 100644 index 000000000..7bb7a6f26 --- /dev/null +++ b/combination-sum/seungseung88.js @@ -0,0 +1,27 @@ +/** + * 시간복잡도: O(2^n) + * 공간복잡도: O(target) + */ + +function combinationSum(canditates, target) { + const result = []; + const nums = []; + + function dfs(start, total) { + if (total > target) return; + if (total === target) result.push([...nums]); + + // 중복제거를 위해 start로 시작 + for (let i = start; i < canditates.length; i += 1) { + num = canditates[i]; + nums.push(num); + dfs(i, total + num); + nums.pop(); + } + } + + // 시작 인덱스, 누적 합 + dfs(0, 0); + + return result; +} diff --git a/decode-ways/seungseung88.js b/decode-ways/seungseung88.js new file mode 100644 index 000000000..671bfec21 --- /dev/null +++ b/decode-ways/seungseung88.js @@ -0,0 +1,26 @@ +/** + * 시간 복잡도: O(n) + * 공간 복잡도: O(n) + */ +var numDecodings = function (s) { + const memo = new Map(); + memo.set(s.length, 1); + + function dfs(start) { + if (memo.has(start)) { + return memo.get(start); + } + + if (s[start] === '0') { + memo.set(start, 0); + } else if (start + 1 < s.length && parseInt(s.slice(start, start + 2)) < 27) { + memo.set(start, dfs(start + 1) + dfs(start + 2)); + } else { + memo.set(start, dfs(start + 1)); + } + + return memo.get(start); + } + + return dfs(0); +}; diff --git a/merge-two-sorted-lists/seungseung88.js b/merge-two-sorted-lists/seungseung88.js new file mode 100644 index 000000000..9a0cc2616 --- /dev/null +++ b/merge-two-sorted-lists/seungseung88.js @@ -0,0 +1,23 @@ +/** + * 시간 복잡도: O(n + m) + * 공간 복잡도: O(1) + */ +var mergeTwoLists = function (list1, list2) { + const dummy = new ListNode(); + node = dummy; + + while (list1 && list2) { + if (list1.val < list2.val) { + node.next = list1; + list1 = list1.next; + } else { + node.next = list2; + list2 = list2.next; + } + node = node.next; + } + + node.next = list1 || list2; + + return dummy.next; +};