-
-
Notifications
You must be signed in to change notification settings - Fork 211
[uraflower] WEEK 03 solutions #1285
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
5 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,44 @@ | ||
/** | ||
* 주어진 배열의 원소 조합(중복 허용)의 합이 target인 모든 경우를 반환하는 함수 | ||
* @param {number[]} candidates | ||
* @param {number} target | ||
* @return {number[][]} | ||
*/ | ||
const combinationSum = function(candidates, target) { | ||
const sortedCandidates = candidates.filter((x) => x <= target).sort((a, b) => Number(a) - Number(b)); | ||
const answer = []; | ||
|
||
if (sortedCandidates.length === 0) { | ||
return answer; | ||
} | ||
|
||
function search(currentIdx, combination, total) { | ||
if (total === target) { | ||
answer.push([...combination]); // 배열 자체를 넣으면 참조 때문에 값이 변경되므로, 복사해서 넣어야 함 | ||
return; | ||
} | ||
|
||
if (total > target) { | ||
return; // backtracking | ||
} | ||
|
||
combination.push(sortedCandidates[currentIdx]); | ||
search(currentIdx, combination, total + sortedCandidates[currentIdx]); | ||
combination.pop(); | ||
|
||
if (total + sortedCandidates[currentIdx] > target) { | ||
return; // backtracking | ||
} | ||
|
||
if (currentIdx + 1 < sortedCandidates.length) { | ||
search(currentIdx + 1, combination, total); | ||
} | ||
} | ||
|
||
search(0, [], 0); | ||
return answer; | ||
}; | ||
|
||
// t: target | ||
// 시간복잡도: O(2^t) | ||
// 공간복잡도: O(t) | ||
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,36 @@ | ||
/** | ||
* 주어진 문자열을 복호화하는 경우의 수를 반환하는 함수 | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
const numDecodings = function(s) { | ||
const dp = {}; | ||
|
||
function decode(idx) { | ||
if (s[idx] === '0') { | ||
return 0; | ||
} | ||
|
||
if (idx === s.length) { | ||
return 1; | ||
} | ||
|
||
if (dp[idx]) { | ||
return dp[idx]; | ||
} | ||
|
||
let result = 0; | ||
result += decode(idx + 1); // 현재 문자만 쓰는 경우: 다음 문자부터 탐색 | ||
if (s[idx + 1] && Number(s[idx] + s[idx+1]) <= 26) { | ||
result += decode(idx + 2); // 현재 문자와 다음 문자 붙여서 쓰는 경우: 다다음 문자부터 탐색 | ||
} | ||
|
||
dp[idx] = result; | ||
return result; | ||
} | ||
|
||
return decode(0); | ||
}; | ||
|
||
// 시간복잡도: O(n) (메모이제이션 안하면 매 인덱스마다 최대 2개의 하위 호출이 발생하여 O(2^n)) | ||
// 공간복잡도: O(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,22 @@ | ||
/** | ||
* 주어진 배열에서 원소의 합이 가장 큰 부분 배열의 합을 반환하는 함수 | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
const maxSubArray = function(nums) { | ||
let max = nums[0]; | ||
let subSum = 0; | ||
|
||
nums.forEach((num) => { | ||
subSum = Math.max(subSum + num, num); | ||
|
||
if (max < subSum) { | ||
max = subSum; | ||
} | ||
}); | ||
|
||
return max; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) |
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,12 @@ | ||
/** | ||
* 주어진 숫자를 2진수로 표현했을 때 비트가 1인 개수를 반환하는 함수 | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
const hammingWeight = function(n) { | ||
const binary = n.toString(2); | ||
return Array.from(binary).filter((bit) => bit == 1).length; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(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,26 @@ | ||
/** | ||
* 주어진 문자열이 조건을 만족하는 회문인지 여부를 반환하는 함수 | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
const isPalindrome = function(s) { | ||
const filtered = Array.from(s.toLowerCase()).reduce((str, char) => { | ||
return isAlphanumeric(char) ? str + char : str; | ||
}, ''); | ||
|
||
for (let left = 0, right = filtered.length - 1; left < right; left++, right--) { | ||
if (filtered[left] !== filtered[right]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
|
||
function isAlphanumeric(char) { | ||
return char !== ' ' && (('a' <= char && char <= 'z') || !Number.isNaN(Number(char))); | ||
} | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) |
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.
복잡도 계산이 올바른지 잘 모르겠습니다..!
Uh oh!
There was an error while loading. Please reload this page.
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.
제가 알고리즘 풀이가 처음이라 복잡도 계산을 잘 모릅니다... 같이 해드릴수가 없어서 죄송합니다ㅠㅠ