-
-
Notifications
You must be signed in to change notification settings - Fork 195
[krokerdile] WEEK 03 solutions #1312
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
6 commits
Select commit
Hold shift + click to select a range
7f114c6
valid-padindrome solution
krokerdile 4ab5a03
number of 1 bits solution
krokerdile 0e7bd78
combination-sum solution
krokerdile 3864ab5
decode ways solution
krokerdile f198072
maximum subarray solution
krokerdile 11df4e1
개행문자 추가
krokerdile 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,26 @@ | ||
/** | ||
* @param {number[]} candidates | ||
* @param {number} target | ||
* @return {number[][]} | ||
*/ | ||
var combinationSum = function(candidates, target) { | ||
const result = []; | ||
|
||
const dfs = (start, path, sum) => { | ||
if (sum === target) { | ||
result.push([...path]); // 정답 조합 발견 | ||
return; | ||
} | ||
|
||
if (sum > target) return; // target 초과 -> 백트랙 | ||
|
||
for (let i = start; i < candidates.length; i++) { | ||
path.push(candidates[i]); // 숫자 선택 | ||
dfs(i, path, sum + candidates[i]); // 같은 인덱스부터 다시 탐색 (중복 사용 허용) | ||
path.pop(); // 백트래킹 | ||
} | ||
}; | ||
|
||
dfs(0, [], 0); | ||
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,27 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var numDecodings = function(s) { | ||
const n = s.length; | ||
const memo = {}; | ||
|
||
function dfs(index) { | ||
if (index === n) return 1; | ||
if (s[index] === '0') return 0; | ||
if (memo.hasOwnProperty(index)) return memo[index]; | ||
|
||
let count = dfs(index + 1); | ||
if (index + 1 < n) { | ||
const twoDigit = parseInt(s.slice(index, index + 2)); | ||
if (twoDigit >= 10 && twoDigit <= 26) { | ||
count += dfs(index + 2); | ||
} | ||
} | ||
|
||
memo[index] = count; | ||
return count; | ||
} | ||
|
||
return dfs(0); | ||
}; |
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,16 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubArray = function(nums) { | ||
let maxSum = nums[0]; | ||
let currentSum = nums[0]; | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
// 지금 원소 자체가 더 클 수도 있음 (부분합 리셋) | ||
currentSum = Math.max(nums[i], currentSum + nums[i]); | ||
maxSum = Math.max(maxSum, currentSum); | ||
} | ||
|
||
return maxSum; | ||
}; |
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,14 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var hammingWeight = function(n) { | ||
let list = n.toString(2).split('').map(Number); | ||
let sum = 0; | ||
list.forEach((m)=>{ | ||
if(m == 1){ | ||
sum += 1;a | ||
} | ||
}) | ||
return sum; | ||
}; |
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,9 @@ | ||
function filterStr(inputString) { | ||
return inputString.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); | ||
} | ||
|
||
var isPalindrome = function(s) { | ||
const filtered = filterStr(s); | ||
const reversed = filtered.split('').reverse().join(''); | ||
return filtered === reversed; | ||
}; |
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.
dfs와 dp를 사용하여 시간복잡도 O(n) 공간복잡도 O(n) 풀이를 보고 저도 많은 도움이 되었습니다. 이번 주차도 고생하셨습니다~!
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.
감사합니다!