Skip to content

Commit 11e2561

Browse files
authored
Merge pull request #1351 from seungseung88/main
[seungseung88] Week 4 Solutions
2 parents 21482c2 + 3ef5a41 commit 11e2561

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

combination-sum/seungseung88.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 시간복잡도: O(2^n)
3+
* 공간복잡도: O(target)
4+
*/
5+
6+
function combinationSum(canditates, target) {
7+
const result = [];
8+
const nums = [];
9+
10+
function dfs(start, total) {
11+
if (total > target) return;
12+
if (total === target) result.push([...nums]);
13+
14+
// 중복제거를 위해 start로 시작
15+
for (let i = start; i < canditates.length; i += 1) {
16+
num = canditates[i];
17+
nums.push(num);
18+
dfs(i, total + num);
19+
nums.pop();
20+
}
21+
}
22+
23+
// 시작 인덱스, 누적 합
24+
dfs(0, 0);
25+
26+
return result;
27+
}

decode-ways/seungseung88.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(n)
4+
*/
5+
var numDecodings = function (s) {
6+
const memo = new Map();
7+
memo.set(s.length, 1);
8+
9+
function dfs(start) {
10+
if (memo.has(start)) {
11+
return memo.get(start);
12+
}
13+
14+
if (s[start] === '0') {
15+
memo.set(start, 0);
16+
} else if (start + 1 < s.length && parseInt(s.slice(start, start + 2)) < 27) {
17+
memo.set(start, dfs(start + 1) + dfs(start + 2));
18+
} else {
19+
memo.set(start, dfs(start + 1));
20+
}
21+
22+
return memo.get(start);
23+
}
24+
25+
return dfs(0);
26+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 시간 복잡도: O(n + m)
3+
* 공간 복잡도: O(1)
4+
*/
5+
var mergeTwoLists = function (list1, list2) {
6+
const dummy = new ListNode();
7+
node = dummy;
8+
9+
while (list1 && list2) {
10+
if (list1.val < list2.val) {
11+
node.next = list1;
12+
list1 = list1.next;
13+
} else {
14+
node.next = list2;
15+
list2 = list2.next;
16+
}
17+
node = node.next;
18+
}
19+
20+
node.next = list1 || list2;
21+
22+
return dummy.next;
23+
};

0 commit comments

Comments
 (0)