-
-
Notifications
You must be signed in to change notification settings - Fork 339
[sadie100] WEEK 03 Solutions #2438
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
+114
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f70016b
sadie100: valid palindrome solution
sadie100 1f732f5
Merge branch 'DaleStudy:main' into main
sadie100 0439ac7
sadie100: number of 1 bits solution
sadie100 af27ccb
sadie100: combination sum solution
sadie100 c13a9eb
sadie100: decode ways solution
sadie100 e86ed20
sadie100 : add line
sadie100 09beca4
sadie100: maximum subarray solution
sadie100 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 @@ | ||
| /* | ||
| dfs로 각 candidates를 돌면서 target과 같아지는 순간에 result를 반환한다. | ||
|
|
||
| 같은 조합이 중복됨을 피하기 위해 candidates를 정렬하고, 각 숫자보다 크거나 같은 candidate만 탐색한다. | ||
|
|
||
| 시간복잡도 : O(N^(T/min(c))) - N은 candidates의 수, T는 타겟, c는 candidates. | ||
| 타겟을 candidates의 최소값으로 나눈 값이 최대 깊이이므로 최악의 경우 해당 횟수만큼 반복해서 배열을 탐색함 | ||
| */ | ||
|
|
||
| function combinationSum(candidates: number[], target: number): number[][] { | ||
| candidates.sort((a, b) => a - b) | ||
| const result = [] | ||
| const search = (idx, nums, sum) => { | ||
| if (sum === target) { | ||
| result.push(nums) | ||
| return | ||
| } | ||
| for (let i = idx; i < candidates.length; i++) { | ||
| const num = candidates[i] | ||
| if (sum + num > target) return | ||
| search(i, [...nums, num], sum + num) | ||
| } | ||
| } | ||
|
|
||
| search(0, [], 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,30 @@ | ||
| /* | ||
| dp 배열을 만들고 s를 순회하며 각 단계에서 이전 자리의 숫자를 채택하거나 버리는 경우를 더해 나간다 | ||
| 만약 현재 순번의 숫자가 0일 경우 : 이전 자리의 숫자를 채택, i-1 순번의 숫자가 0이거나 3 이상이면 즉시 0을 리턴. 아닐 경우 dp의 i-2번째값을 넣는다. | ||
| 이전 순번의 숫자와 현재 숫자를 더한 값이 26보다 크거나 이전 순번의 숫자가 0일 경우 : 개별 자리의 숫자를 채택, 직전 dp값을 넣는다 | ||
| 위에 모두 해당하지 않을 경우 : 개별 자리의 숫자를 채택하거나 버리는 경우를 다 고려하여 직전 dp값과 2스텝 전 dp값을 더한다 | ||
| 맨 끝 순번에 도달했을 때의 값을 구한다 | ||
|
|
||
| 시간복잡도 : O(N) (N은 s의 길이) | ||
| */ | ||
|
|
||
| function numDecodings(s: string): number { | ||
| if(s.startsWith("0")) return 0; | ||
| const dp = []; | ||
|
|
||
| dp.push(1); | ||
| if(s.length<2) return dp[0] | ||
| for(let i=1;i<s.length;i++){ | ||
| const numStr = s[i]; | ||
| if(numStr === "0") { | ||
| if(s[i-1] === "0" || Number(s[i-1])>=3) return 0; | ||
| dp.push(dp?.[i-2] ?? 1) | ||
| }else if(Number(`${s[i-1]}${numStr}`) > 26 || s[i-1] === "0"){ | ||
| dp.push(dp[i-1]) | ||
| }else{ | ||
| dp.push(dp[i-1] + (dp?.[i-2] ?? 1)) | ||
| } | ||
| } | ||
|
|
||
| return dp[s.length-1]; | ||
| }; |
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 @@ | ||
| /* | ||
| nums를 순회하며 최대 sum을 찾는다. 다음 규칙을 따른다 | ||
| - 매 순간 숫자를 더해 가며 현재까지의 합과 최대합 비교, 갱신 | ||
| - 만약 해당 인덱스 num을 더했을 때 0보다 작아지면 현재까지의 값을 0으로 초기화(총합 버림) | ||
|
|
||
| */ | ||
|
|
||
| function maxSubArray(nums: number[]): number { | ||
| let result = -Infinity; | ||
| let curSum = 0; | ||
|
|
||
| for (let num of nums) { | ||
| curSum += num; | ||
| result = Math.max(result, curSum); | ||
| if (curSum < 0) curSum = 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,14 @@ | ||
| /** | ||
| n을 이진수 변환하고 1의 개수를 센다 | ||
|
|
||
| 시간복잡도 O(N) (N은 숫자 n의 비트 개수) | ||
| */ | ||
|
|
||
| function hammingWeight(n: number): number { | ||
| const binaryNum = n.toString(2) | ||
| let result = 0 | ||
| for (let char of binaryNum) { | ||
| if (char === '1') result += 1 | ||
| } | ||
| 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,23 @@ | ||
| /* | ||
| s에서 알파벳과 숫자가 아닌 글자를 다 날리고, 스택에 문자를 절반까지 담은 후 절반 이후부터는 꺼내가며 앞뒤 레터가 일치하는지 확인한다 | ||
|
|
||
| 시간복잡도 : O(N) (N은 s의 길이) | ||
| 공간복잡도 : O(N) (스택) | ||
| */ | ||
|
|
||
| function isPalindrome(s: string): boolean { | ||
| const newStr = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase() | ||
| const isStrEven = newStr.length % 2 === 0 | ||
| const lenHalf = Math.floor(newStr.length / 2) | ||
| const stack = [] | ||
| for (let i = 0; i < newStr.length; i++) { | ||
| if (i < lenHalf) { | ||
| stack.push(newStr[i]) | ||
| } else { | ||
| if (!isStrEven && i === lenHalf) continue | ||
|
Comment on lines
+12
to
+17
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. 배열 절반 부분을 미리 slice 함수 등으로 잘라서 stack 에 담아두는 방법도 있을 것 같아요! |
||
| const isSame = newStr[i] === stack.pop() | ||
| if (!isSame) 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.
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.
👍