Skip to content

[seungseung88] Week 4 Solutions #1351

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 3 commits 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
27 changes: 27 additions & 0 deletions combination-sum/seungseung88.js
Original file line number Diff line number Diff line change
@@ -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;
}
26 changes: 26 additions & 0 deletions decode-ways/seungseung88.js
Original file line number Diff line number Diff line change
@@ -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);
};
23 changes: 23 additions & 0 deletions merge-two-sorted-lists/seungseung88.js
Original file line number Diff line number Diff line change
@@ -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;
};