Skip to content

Commit 13a37fd

Browse files
authored
Merge pull request DaleStudy#2440 from Cyjin-jani/main
[Cyjin-jani] WEEK 03 solutions
2 parents d8e44f5 + 91c1722 commit 13a37fd

5 files changed

Lines changed: 164 additions & 0 deletions

File tree

combination-sum/Cyjin-jani.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ! 나중에 다시 풀어야 할 문제
2+
//! 30분 내에 풀지 못해서 AI의 도움을 받아 구현했습니다..
3+
// recursion의 사용, 종료 조건 등 어느정도 근접한 아이디어를 구현했지만,
4+
// idx를 넘기지 않고 number를 직접 넘기려고 했던 부분이나, results pop을 놓쳤던 부분 등이 있습니다.
5+
const combinationSum = function (candidates, target) {
6+
const answer = [];
7+
8+
function recursion(idx, target, results = []) {
9+
if (target === 0) {
10+
answer.push([...results]);
11+
return;
12+
}
13+
if (target < 0) return;
14+
15+
for (let i = idx; i < candidates.length; i++) {
16+
results.push(candidates[i]);
17+
recursion(i, target - candidates[i], results);
18+
results.pop();
19+
}
20+
}
21+
recursion(0, target, []);
22+
23+
return answer;
24+
};

decode-ways/Cyjin-jani.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//! 다시 풀어야 하는 문제
2+
3+
// Time limit 초과
4+
const numDecodings_timeLimit = function (s) {
5+
let answer = 0;
6+
7+
function recursion(idx, remains) {
8+
if (idx === remains.length) {
9+
answer++;
10+
return;
11+
}
12+
13+
const one = remains.slice(idx, idx + 1);
14+
if (+one >= 1 && +one <= 9) {
15+
recursion(idx + 1, remains);
16+
}
17+
18+
if (idx + 1 < remains.length) {
19+
const two = remains.slice(idx, idx + 2);
20+
if (+two >= 10 && +two <= 26) {
21+
recursion(idx + 2, remains);
22+
}
23+
}
24+
}
25+
26+
recursion(0, s);
27+
28+
return answer;
29+
};
30+
31+
// 메모이제이션 적용
32+
const numDecodings = function (s) {
33+
function recursion(idx, remains, memo = {}) {
34+
if (idx === remains.length) return 1;
35+
36+
if (idx in memo) return memo[idx];
37+
38+
let answer = 0;
39+
40+
const one = remains.slice(idx, idx + 1);
41+
if (+one >= 1 && +one <= 9) {
42+
answer += recursion(idx + 1, remains, memo);
43+
}
44+
45+
if (idx + 1 < remains.length) {
46+
const two = remains.slice(idx, idx + 2);
47+
if (+two >= 10 && +two <= 26) {
48+
answer += recursion(idx + 2, remains, memo);
49+
}
50+
}
51+
52+
memo[idx] = answer;
53+
return answer;
54+
}
55+
56+
return recursion(0, s, {});
57+
};

maximum-subarray/Cyjin-jani.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! 다시 풀어보기 (divide and conquer)
2+
const maxSubArray = function (nums) {
3+
let currentSum = 0;
4+
let maxSum = -Infinity;
5+
6+
for (let right = 0; right < nums.length; right++) {
7+
currentSum += nums[right];
8+
maxSum = Math.max(maxSum, currentSum);
9+
if (currentSum < 0) currentSum = 0;
10+
}
11+
12+
return maxSum;
13+
};

number-of-1-bits/Cyjin-jani.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// tc: O(logn)
2+
// sc: O(logn)
3+
const hammingWeight_simple = function (n) {
4+
const binaryStr = n.toString(2);
5+
let answer = 0;
6+
7+
for (char of binaryStr) {
8+
if (+char === 1) {
9+
answer++;
10+
}
11+
}
12+
13+
return answer;
14+
};
15+
16+
// toString(2)를 쓰지 않고 처리
17+
// tc: O(logn)
18+
// sc: O(1)
19+
const hammingWeight = function (n) {
20+
let answer = 0;
21+
22+
while (n >= 0) {
23+
const remains = n % 2;
24+
if (remains === 1) answer++;
25+
n = Math.floor(n / 2);
26+
}
27+
28+
return answer;
29+
};

valid-palindrome/Cyjin-jani.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// naive한 풀이
2+
// tc: O(n)
3+
// sc: O(n)
4+
const isPalindromeNaive = function (s) {
5+
const str = s.toLowerCase().replace(/[^a-z0-9]/g, ''); // 이 부분에서 공간복잡도가 O(n)
6+
let leftIdx = 0;
7+
let rightIdx = str.length - 1;
8+
9+
while (leftIdx <= rightIdx) {
10+
if (str[leftIdx] !== str[rightIdx]) {
11+
return false;
12+
} else {
13+
leftIdx++;
14+
rightIdx--;
15+
}
16+
}
17+
return true;
18+
};
19+
20+
// 좀 더 최적화 된 풀이
21+
// tc: O(n)
22+
// cs: O(1)
23+
const isPalindrome = function (s) {
24+
let leftIdx = 0;
25+
let rightIdx = s.length - 1;
26+
27+
while (leftIdx < rightIdx) {
28+
while (leftIdx < rightIdx && !isAlphaNumeric(s[leftIdx])) leftIdx++;
29+
while (leftIdx < rightIdx && !isAlphaNumeric(s[rightIdx])) rightIdx--;
30+
31+
if (s[leftIdx].toLowerCase() !== s[rightIdx].toLowerCase()) return false;
32+
33+
leftIdx++;
34+
rightIdx--;
35+
}
36+
return true;
37+
};
38+
39+
function isAlphaNumeric(char) {
40+
return /[a-zA-Z0-9]/.test(char);
41+
}

0 commit comments

Comments
 (0)