-
-
Notifications
You must be signed in to change notification settings - Fork 339
[Cyjin-jani] WEEK 03 solutions #2440
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
8d30b5f
f886f0b
cf6e2dc
ddddb86
91c1722
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,24 @@ | ||
| // ! 나중에 다시 풀어야 할 문제 | ||
| //! 30분 내에 풀지 못해서 AI의 도움을 받아 구현했습니다.. | ||
| // recursion의 사용, 종료 조건 등 어느정도 근접한 아이디어를 구현했지만, | ||
| // idx를 넘기지 않고 number를 직접 넘기려고 했던 부분이나, results pop을 놓쳤던 부분 등이 있습니다. | ||
| const combinationSum = function (candidates, target) { | ||
| const answer = []; | ||
|
|
||
| function recursion(idx, target, results = []) { | ||
| if (target === 0) { | ||
| answer.push([...results]); | ||
| return; | ||
| } | ||
| if (target < 0) return; | ||
|
|
||
| for (let i = idx; i < candidates.length; i++) { | ||
| results.push(candidates[i]); | ||
| recursion(i, target - candidates[i], results); | ||
| results.pop(); | ||
| } | ||
| } | ||
| recursion(0, target, []); | ||
|
|
||
| return answer; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| //! 다시 풀어야 하는 문제 | ||
|
|
||
| // Time limit 초과 | ||
| const numDecodings_timeLimit = function (s) { | ||
| let answer = 0; | ||
|
|
||
| function recursion(idx, remains) { | ||
| if (idx === remains.length) { | ||
| answer++; | ||
| return; | ||
| } | ||
|
|
||
| const one = remains.slice(idx, idx + 1); | ||
| if (+one >= 1 && +one <= 9) { | ||
| recursion(idx + 1, remains); | ||
| } | ||
|
|
||
| if (idx + 1 < remains.length) { | ||
| const two = remains.slice(idx, idx + 2); | ||
| if (+two >= 10 && +two <= 26) { | ||
| recursion(idx + 2, remains); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| recursion(0, s); | ||
|
|
||
| return answer; | ||
| }; | ||
|
|
||
| // 메모이제이션 적용 | ||
| const numDecodings = function (s) { | ||
| function recursion(idx, remains, memo = {}) { | ||
| if (idx === remains.length) return 1; | ||
|
|
||
| if (idx in memo) return memo[idx]; | ||
|
|
||
| let answer = 0; | ||
|
|
||
| const one = remains.slice(idx, idx + 1); | ||
| if (+one >= 1 && +one <= 9) { | ||
| answer += recursion(idx + 1, remains, memo); | ||
| } | ||
|
|
||
| if (idx + 1 < remains.length) { | ||
| const two = remains.slice(idx, idx + 2); | ||
| if (+two >= 10 && +two <= 26) { | ||
| answer += recursion(idx + 2, remains, memo); | ||
| } | ||
| } | ||
|
|
||
| memo[idx] = answer; | ||
| return answer; | ||
| } | ||
|
|
||
| return recursion(0, s, {}); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| //! 다시 풀어보기 (divide and conquer) | ||
| const maxSubArray = function (nums) { | ||
| let currentSum = 0; | ||
| let maxSum = -Infinity; | ||
|
|
||
| for (let right = 0; right < nums.length; right++) { | ||
| currentSum += nums[right]; | ||
| maxSum = Math.max(maxSum, currentSum); | ||
| if (currentSum < 0) currentSum = 0; | ||
| } | ||
|
|
||
| return maxSum; | ||
| }; |
|
Contributor
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. string으로 처음에 풀이하신 후, 이 문제를 비트를 조작해서 풀어보시는 것도 추천드립니다! 시간복잡도나 공간복잡도 면에서는 비트로 풀어도 동일합니다.
Contributor
Author
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,29 @@ | ||
| // tc: O(logn) | ||
| // sc: O(logn) | ||
| const hammingWeight_simple = function (n) { | ||
| const binaryStr = n.toString(2); | ||
| let answer = 0; | ||
|
|
||
| for (char of binaryStr) { | ||
| if (+char === 1) { | ||
| answer++; | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| }; | ||
|
|
||
| // toString(2)를 쓰지 않고 처리 | ||
| // tc: O(logn) | ||
| // sc: O(1) | ||
| const hammingWeight = function (n) { | ||
| let answer = 0; | ||
|
|
||
| while (n >= 0) { | ||
| const remains = n % 2; | ||
| if (remains === 1) answer++; | ||
| n = Math.floor(n / 2); | ||
| } | ||
|
|
||
| return answer; | ||
| }; |
|
Contributor
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,41 @@ | ||
| // naive한 풀이 | ||
| // tc: O(n) | ||
| // sc: O(n) | ||
| const isPalindromeNaive = function (s) { | ||
| const str = s.toLowerCase().replace(/[^a-z0-9]/g, ''); // 이 부분에서 공간복잡도가 O(n) | ||
| let leftIdx = 0; | ||
| let rightIdx = str.length - 1; | ||
|
|
||
| while (leftIdx <= rightIdx) { | ||
| if (str[leftIdx] !== str[rightIdx]) { | ||
| return false; | ||
| } else { | ||
| leftIdx++; | ||
| rightIdx--; | ||
| } | ||
| } | ||
| return true; | ||
| }; | ||
|
|
||
| // 좀 더 최적화 된 풀이 | ||
| // tc: O(n) | ||
| // cs: O(1) | ||
| const isPalindrome = function (s) { | ||
| let leftIdx = 0; | ||
| let rightIdx = s.length - 1; | ||
|
|
||
| while (leftIdx < rightIdx) { | ||
| while (leftIdx < rightIdx && !isAlphaNumeric(s[leftIdx])) leftIdx++; | ||
| while (leftIdx < rightIdx && !isAlphaNumeric(s[rightIdx])) rightIdx--; | ||
|
|
||
| if (s[leftIdx].toLowerCase() !== s[rightIdx].toLowerCase()) return false; | ||
|
|
||
| leftIdx++; | ||
| rightIdx--; | ||
| } | ||
| return true; | ||
| }; | ||
|
|
||
| function isAlphaNumeric(char) { | ||
| return /[a-zA-Z0-9]/.test(char); | ||
| } |
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.
백트래킹 쉽지 않네요. 저도..AI 도움을 많이 받았습니다. 🥲