-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jun0811] WEEK 03 solutions #1787
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @param {number[]} candidates | ||
* @param {number} target | ||
* @return {number[][]} | ||
*/ | ||
var combinationSum = function (candidates, target) { | ||
const result = []; | ||
candidates.sort((a, b) => a - b); | ||
function backTracking(start, arr, total) { | ||
if (total == target) { | ||
result.push([...arr]); | ||
return; | ||
} | ||
|
||
for (let i = start; i < candidates.length; i++) { | ||
const cur = candidates[i]; | ||
if (cur + total > target) { | ||
return; | ||
} | ||
|
||
backTracking(i, [...arr, cur], total + cur); | ||
} | ||
} | ||
|
||
backTracking(0, [], 0); | ||
return result; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
|
||
function check(str) { | ||
if (str.length == 2) { | ||
if (str[0] == '0') return false; | ||
} | ||
if (str >= 1 && str <= 26) return true; | ||
return false; | ||
} | ||
|
||
var numDecodings = function (s) { | ||
if (s[0] == '0') return 0; | ||
const dp = [1, 1]; | ||
|
||
for (let i = 2; i <= s.length; i++) { | ||
let tmp = dp[i - 1] + dp[i - 2]; | ||
|
||
// 2글자 체크 -> dp[i-2]을 뺴줌 | ||
const two_c = String(s[i - 2]) + String(s[i - 1]); | ||
if (!check(two_c)) tmp -= dp[i - 2]; | ||
|
||
// 1글자 체크 -> dp[i-1]을 빼줌 | ||
if (!check(s[i - 1])) tmp -= dp[i - 1]; | ||
|
||
if (tmp == 0) return 0; | ||
dp[i] = tmp; | ||
} | ||
return dp[s.length]; | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dp 배열을 사용해 문제를 풀이해주셨군요! 배열을 사용하지 않고 변수만 사용해서 공간복잡도를 줄이는 방법도 있습니다. 다음에 한 번 고려해보면 좋을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 감사합니다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubArray = function (nums) { | ||
const dp = [nums[0]]; | ||
for (let i = 1; i < nums.length; i++) { | ||
const cur = nums[i]; | ||
if (dp[i - 1] < 0) dp[i] = cur; | ||
else { | ||
dp[i] = dp[i - 1] + cur; | ||
} | ||
} | ||
return Math.max(...dp); | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isAlphaNumeric() 이라는 함수를 정의하니 코드가 분리돼서 보기 편했습니다. 아스키코드로 풀이를 하셨네요! 정규표현식을 사용해서도 풀이할 수 있을 거 같습니다. 다음에 고려해보시면 좋을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사합니다. 정규표현식이 익숙하지 않아서 그냥 했는데 익숙하게 만들어야겠네요 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var isPalindrome = function (s) { | ||
let left = 0; | ||
let right = s.length - 1; | ||
|
||
while (left < right) { | ||
while (left < right && !isAlphaNumeric(s[left])) { | ||
left++; | ||
} | ||
|
||
while (left < right && !isAlphaNumeric(s[right])) { | ||
right--; | ||
} | ||
|
||
if (s[left].toLowerCase() !== s[right].toLowerCase()) { | ||
return false; | ||
} | ||
|
||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
function isAlphaNumeric(char) { | ||
const code = char.charCodeAt(0); | ||
return ( | ||
(code >= 48 && code <= 57) || // 0-9 | ||
(code >= 65 && code <= 90) || // A-Z | ||
(code >= 97 && code <= 122) | ||
); // a-z | ||
} |
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.
total과 target을☺️
===
연산자로도 비교할 수 있을 것 같습니다!==
연산자를 사용한 이유가 궁금합니다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.
알고리즘 문제를 풀때는 엄격한 비교가 방해될때가 있어서 그렇게 하는 편인데.. 안좋은 습관일까요