-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
가지치기 과정을 통해 이미 목표 값보다 넘는 경우 후보군에서 제거하는 부분이 인상적이었습니다. 저는 백트래킹 알고리즘이 아직 익숙하지 않은데 사전에 불필요한 경로를 차단하여 모든 후보 검사를 하지 않는 전략을 고려해야 한다는 점을 배웠습니다.
3주차도 고생 많으셨고, 다음 주도 같이 화이팅입니다!