-
-
Notifications
You must be signed in to change notification settings - Fork 230
[jeongwoo903] Week 03 solutions #1299
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
7 commits
Select commit
Hold shift + click to select a range
e9f1cb4
solve(w03): 125. Valid Palindrome
jeongwoo903 534c9c6
fix: 줄바꿈 누락
jeongwoo903 779d845
solve(w03): 191. Number of 1 Bits
jeongwoo903 48e375b
solve(w03): 39. Combination Sum
jeongwoo903 d748504
solve(w03): 91. Decode Ways
jeongwoo903 57cd633
solve(w03): 53. Maximum Subarray
jeongwoo903 e7c0e90
fix: 줄바꿈 누락
jeongwoo903 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,29 @@ | ||
/* | ||
* k: 후보 수 | ||
* t: target / 최소 숫자 | ||
* 시간 복잡도: O(k^t) | ||
* 공간 복잡도; O(k^t * t) | ||
*/ | ||
|
||
/** | ||
* @param {number[]} candidates | ||
* @param {number} target | ||
* @return {number[][]} | ||
*/ | ||
|
||
var combinationSum = function(candidates, target) { | ||
const combine = (sum, path, startIndex) => { | ||
if (sum > target) return []; | ||
if (sum === target) return [path]; | ||
|
||
return candidates | ||
.slice(startIndex) | ||
.flatMap((candidate, i) => | ||
combine(sum + candidate, [...path, candidate], startIndex + i) | ||
); | ||
}; | ||
|
||
return candidates.flatMap((candidate, i) => | ||
combine(candidate, [candidate], i) | ||
); | ||
}; |
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 @@ | ||
/* | ||
* 시간 복잡도: O(n) | ||
* 공간 복잡도; O(n) | ||
*/ | ||
|
||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var numDecodings = function(s) { | ||
if (s.length === 0 || s[0] === "0") return 0; | ||
|
||
let dp = new Array(s.length + 1).fill(0); | ||
|
||
dp[0] = 1; | ||
dp[1] = 1; | ||
|
||
for (let i = 2; i <= s.length; i++) { | ||
let single = s[i - 1]; | ||
let double = s[i - 2] + s[i - 1]; | ||
|
||
if (single >= 1 && single <= 9) dp[i] += dp[i - 1]; | ||
if (double >= 10 && double <= 26) dp[i] += dp[i - 2]; | ||
} | ||
|
||
return dp[s.length]; | ||
}; |
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,21 @@ | ||
/* | ||
* 시간 복잡도: O(n) | ||
* 공간 복잡도: O(1) | ||
*/ | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubArray = function(nums) { | ||
let max = nums[0]; | ||
let result = nums[0]; | ||
|
||
for (let index = 1; index < nums.length; index++) { | ||
const num = nums[index]; | ||
result = Math.max(result + num, num); | ||
max = Math.max(max, result); | ||
} | ||
|
||
return max; | ||
}; |
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 @@ | ||
/* | ||
* 시간 복잡도: O(log n) | ||
* 공간 복잡도; O(log n) | ||
*/ | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var hammingWeight = function(n) { | ||
const dec = n.toString(2); | ||
const parsedBits = [...dec].filter(item => item === '1'); | ||
return parsedBits.length; | ||
}; |
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,20 @@ | ||
/* | ||
* 시간 복잡도: O(n) | ||
* 공간 복잡도; O(n) | ||
* | ||
* 과정: | ||
* 1. 소문자, 대문자, 숫자만 파싱 | ||
* 2. 소문자로 변환 | ||
* 3. 리버스 스트링과 기본 스트링과 대조 | ||
*/ | ||
|
||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isPalindrome = function(s) { | ||
const parsedString = s.replace(/[^a-zA-Z0-9]/g, ''); | ||
const lowerString = parsedString.toLowerCase(); | ||
const reverseString = lowerString.split("").reverse().join(""); | ||
return reverseString === lowerString; | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.