-
-
Notifications
You must be signed in to change notification settings - Fork 361
[j2h30728] WEEK 03 Solutions #2736
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
+112
−0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
86fccd0
week3: 125. Valid Palindrome
j2h30728 965f5e1
week3: 191. Number of 1 Bits
j2h30728 817cf7c
week3: 39. Combination Sum
j2h30728 ba1bf83
week3:91. Decode Ways
j2h30728 0444130
week3: 53. Maximum Subarray
j2h30728 a857901
[docs](week3): 시간 복잡도 설명 수정
j2h30728 7b434ce
[fix](valid-palindrome): 유효한 회문 검사 로직 수정
j2h30728 f9c1203
[fix](number-of-1-bits): 해밍 가중치 계산 로직 수정
j2h30728 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,28 @@ | ||
| /** | ||
| * 시간 복잡도: O(k * n^k) (n: candidates 길이, k: target / min(candidates) 최대 깊이) | ||
| * 공간 복잡도: O(k) | ||
| */ | ||
| function combinationSum(candidates: number[], target: number): number[][] { | ||
| const result: number[][] = []; | ||
| const current: number[] = []; | ||
|
|
||
| function backtracking(index: number, remain: number) { | ||
| if (remain === 0) { | ||
| result.push([...current]); | ||
| return; | ||
| } | ||
|
|
||
| if (remain < 0) { | ||
| return; | ||
| } | ||
|
|
||
| for (let i = index; i < candidates.length; i++) { | ||
| current.push(candidates[i]); | ||
| backtracking(i, remain - candidates[i]); | ||
| current.pop(); | ||
| } | ||
| } | ||
|
|
||
| backtracking(0, target); | ||
| 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,24 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| function numDecodings(s: string): number { | ||
| const dp = new Array(s.length + 1).fill(0); | ||
| dp[0] = 1; | ||
| dp[1] = parseInt(s[0]) === 0 ? 0 : 1; | ||
|
|
||
| for (let i = 2; i <= s.length; i++) { | ||
| dp[i] = 0; | ||
|
|
||
| if (parseInt(s[i - 1]) !== 0) { | ||
| dp[i] += dp[i - 1]; | ||
| } | ||
| if ( | ||
| parseInt(s.slice(i - 2, i)) <= 26 && | ||
| parseInt(s.slice(i - 2, i)) >= 10 | ||
| ) { | ||
| 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,28 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| function maxSubArray1(nums: number[]): number { | ||
| const dp = new Array(nums.length).fill(0); | ||
| dp[0] = nums[0]; | ||
|
|
||
| for (let i = 1; i < nums.length; i++) { | ||
| dp[i] += Math.max(dp[i - 1] + nums[i], nums[i]); | ||
| } | ||
| return Math.max(...dp); | ||
| } | ||
|
|
||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(1) | ||
| */ | ||
| function maxSubArray2(nums: number[]): number { | ||
| let max = nums[0]; | ||
| let current = nums[0]; | ||
|
|
||
| for (let i = 1; i < nums.length; i++) { | ||
| current = Math.max(current + nums[i], nums[i]); | ||
| max = Math.max(max, current); | ||
| } | ||
| 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,13 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) (n: 입력값의 비트 수) | ||
| * 공간 복잡도: O(1) | ||
| */ | ||
| function hammingWeight(n: number): number { | ||
| let sum = 0; | ||
|
|
||
| while (n > 0) { | ||
| sum += n % 2; | ||
| n = Math.floor(n / 2); | ||
| } | ||
| return (sum += 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 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| function isPalindrome(s: string): boolean { | ||
| let string = ""; | ||
| for (const ch of s) { | ||
| if (/[a-zA-Z0-9]/.test(ch)) { | ||
| string += ch.toLowerCase(); | ||
| } | ||
| } | ||
|
|
||
| for (let i = 0; i < string.length / 2; i++) { | ||
| if (string[i] !== string[string.length - i - 1]) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
Oops, something went wrong.
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.