Skip to content

[moonjonghoo] Week 03 solutions #1306

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 19, 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
28 changes: 28 additions & 0 deletions combination-sum/moonjonghoo.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

가지치기 과정을 통해 이미 목표 값보다 넘는 경우 후보군에서 제거하는 부분이 인상적이었습니다. 저는 백트래킹 알고리즘이 아직 익숙하지 않은데 사전에 불필요한 경로를 차단하여 모든 후보 검사를 하지 않는 전략을 고려해야 한다는 점을 배웠습니다.
3주차도 고생 많으셨고, 다음 주도 같이 화이팅입니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function combinationSum(candidates, target) {
const result = [];

// 1. 정렬: 가지치기를 위한 필수
candidates.sort((a, b) => a - b);

function backtrack(startIndex, path, remaining) {
if (remaining === 0) {
result.push([...path]);
return;
}

for (let i = startIndex; i < candidates.length; i++) {
const current = candidates[i];

// 2. 가지치기
if (current > remaining) break;

// 3. 현재 값 선택
path.push(current);
backtrack(i, path, remaining - current); // i로 재귀 호출: 같은 수 중복 사용 가능
path.pop(); // 4. 백트래킹
}
}

backtrack(0, [], target);
return result;
}
24 changes: 24 additions & 0 deletions decode-ways/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function numDecodings(s) {
const n = s.length;

if (s[0] === "0") return 0;

const dp = new Array(n + 1).fill(0);
dp[0] = 1;
dp[1] = 1;

for (let i = 2; i <= n; i++) {
const oneDigit = Number(s.slice(i - 1, i));
const twoDigits = Number(s.slice(i - 2, i));

if (oneDigit >= 1 && oneDigit <= 9) {
dp[i] += dp[i - 1];
}

if (twoDigits >= 10 && twoDigits <= 26) {
dp[i] += dp[i - 2];
}
}

return dp[n];
}
17 changes: 17 additions & 0 deletions valid-palindrome/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @param {string} s
* @return {boolean}
*/

var isPalindrome = function (s) {
// 1. 영숫자만 남기고, 소문자로 변환
let cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, "");

// 2. 뒤집기
let reversed = cleaned.split("").reverse().join("");

// 3. 비교
return cleaned === reversed;
};

isPalindrome("A man, a plan, a canal: Panama");