-
-
Notifications
You must be signed in to change notification settings - Fork 195
[byol-han] WEEK 03 solutions #1286
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,25 @@ | ||
/** | ||
* @param {number[]} candidates | ||
* @param {number} target | ||
* @return {number[][]} | ||
*/ | ||
var combinationSum = function (candidates, target) { | ||
const result = []; | ||
|
||
function backtrack(remaining, combination, start) { | ||
if (remaining === 0) { | ||
result.push([...combination]); | ||
return; | ||
} | ||
if (remaining < 0) return; | ||
|
||
for (let i = start; i < candidates.length; i++) { | ||
combination.push(candidates[i]); | ||
backtrack(remaining - candidates[i], combination, i); // 같은 숫자 다시 사용 가능 | ||
combination.pop(); // backtrack | ||
} | ||
} | ||
|
||
backtrack(target, [], 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) { | ||
if (!s || s[0] === "0") return 0; | ||
|
||
const n = s.length; | ||
const dp = Array(n + 1).fill(0); | ||
|
||
dp[0] = 1; // 빈 문자열은 1가지 방법 | ||
dp[1] = 1; // 첫 글자가 0이 아니면 1가지 방법 | ||
|
||
for (let i = 2; i <= n; i++) { | ||
const oneDigit = parseInt(s.slice(i - 1, i)); | ||
const twoDigits = parseInt(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,19 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubArray = function (nums) { | ||
// 초기값 설정: 현재까지의 최대합과 전체 최대합을 배열의 첫 번째 값으로 초기화 | ||
let currentSum = nums[0]; | ||
let maxSum = 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,30 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
//1. divide the number by 2 and count the remainder | ||
var hammingWeight = function (n) { | ||
let count = 0; | ||
while (n > 0) { | ||
if (n % 2 === 1) { | ||
count++; | ||
} | ||
n = Math.floor(n / 2); | ||
} | ||
return count; | ||
}; | ||
|
||
//2. Count the number of set bits (1s) in the binary representation of n | ||
var hammingWeight = function (n) { | ||
return n.toString(2).split("1").length - 1; | ||
}; | ||
|
||
//3. bit manipulation | ||
var hammingWeight = function (n) { | ||
let count = 0; | ||
while (n > 0) { | ||
count += n & 1; // 마지막 비트가 1이면 count++ | ||
n = n >>> 1; // 오른쪽으로 한 비트 이동 (2로 나눔) | ||
} | ||
return count; | ||
}; |
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,32 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
|
||
// 1. Two Pointers | ||
// time complexity: O(n) | ||
// space complexity: O(1) | ||
var isPalindrome = function (s) { | ||
let sRefine = s.toLowerCase().replace(/[^a-z0-9]/g, ""); | ||
let left = 0; | ||
let right = sRefine.length - 1; | ||
|
||
while (left < right) { | ||
if (sRefine[left] !== sRefine[right]) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
// 2. String Manipulation | ||
// time complexity: O(n) | ||
// space complexity: O(n) | ||
var isPalindrome = function (s) { | ||
let refined = s.toLowerCase().replace(/[^a-z0-9]/g, ""); | ||
let reversed = refined.split("").reverse().join(""); | ||
return refined === 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.
Backtracking 이 뭔지... 코드가 어떻게 돌아가는건지... 이해못함. 더 공부해야함.