-
-
Notifications
You must be signed in to change notification settings - Fork 263
[jiji-hoon96] WEEK 03 solutions #1264
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
9 commits
Select commit
Hold shift + click to select a range
10dd5bd
solving Valid Palindrome (#220)
jiji-hoon96 9e25289
Merge branch 'DaleStudy:main' into main
jiji-hoon96 e4a226c
solution Valid Palindrome (#220)
jiji-hoon96 2e3c615
solution Number of 1 Bits (#232)
jiji-hoon96 950ced8
solution Combination Sum (#254)
jiji-hoon96 bdfb3d7
solution Combination Sum (#254)
jiji-hoon96 8bc27b0
solution Decode Ways (#268)
jiji-hoon96 add7376
solution Maximum Subarray (#275)
jiji-hoon96 8cc2ab6
solution Maximum Subarray (#275)
jiji-hoon96 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,49 @@ | ||
| /** | ||
| * | ||
| * @param candidates | ||
| * @param target | ||
| * | ||
| * backtracking 알고리즘으로 문제 해결 | ||
| * | ||
| */ | ||
|
|
||
| function combinationSum(candidates: number[], target: number): number[][] { | ||
| const result : number[][] = []; | ||
| if(candidates.length === 0){ | ||
| return result ; | ||
| } | ||
|
|
||
| candidates.sort((a,b)=> a-b); | ||
|
|
||
| const validCandidates : number[] = candidates.filter(num => num <= target); | ||
|
|
||
| if(validCandidates.length ===0) { | ||
| return result; | ||
| } | ||
|
|
||
| const currentCombination : number[] = []; | ||
|
|
||
| function backtrack (startIndex : number, remainingTarget : number) :void { | ||
| if(remainingTarget === 0){ | ||
| result.push([...currentCombination]); | ||
| return; | ||
| } | ||
|
|
||
| for(let i=startIndex; i<validCandidates.length; i++){ | ||
| const currentNum = validCandidates[i]; | ||
|
|
||
| if(currentNum > remainingTarget) { | ||
| break; | ||
| } | ||
| currentCombination.push(currentNum); | ||
|
|
||
| backtrack(i,remainingTarget - currentNum) | ||
|
|
||
| currentCombination.pop() | ||
|
|
||
| } | ||
| } | ||
|
|
||
| backtrack(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,35 @@ | ||
| function numDecodings(s: string): number { | ||
| // 빈 문자열이거나 0으로 시작하면 디코딩 불가 | ||
| if (!s || s[0] === '0') return 0; | ||
|
|
||
| const n = s.length; | ||
|
|
||
| // 문자열 길이가 1이면 바로 결과 반환 | ||
| if (n === 1) return 1; | ||
|
|
||
| // 초기 상태 | ||
| let prev = 1; // dp[0] | ||
| let curr = s[0] === '0' ? 0 : 1; // dp[1] | ||
|
|
||
| for (let i = 2; i <= n; i++) { | ||
| let temp = 0; | ||
| const oneDigit = parseInt(s[i - 1]); | ||
| const twoDigit = parseInt(s[i - 2] + s[i - 1]); | ||
|
|
||
| // 한 자리 숫자로 디코딩 (1-9) | ||
| if (oneDigit >= 1) { | ||
| temp += curr; | ||
| } | ||
|
|
||
| // 두 자리 숫자로 디코딩 (10-26) | ||
| if (twoDigit >= 10 && twoDigit <= 26) { | ||
| temp += prev; | ||
| } | ||
|
|
||
| // 상태 업데이트 | ||
| prev = curr; | ||
| curr = temp; | ||
|
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. prev, curr 두 변수만으로 상태 저장해서 O(1) 공간복잡도로 해결한 점이 인상깊어요! |
||
| } | ||
|
|
||
| return curr; | ||
| } | ||
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 @@ | ||
| function maxSubArray(nums: number[]): number { | ||
| // 배열이 비어 있는 경우 체크 (제약조건에 의해 발생하지 않지만, 견고한 코드를 위해) | ||
| if (nums.length === 0) return 0; | ||
|
|
||
| // 현재 부분 배열의 합과 전체 최대 부분 배열 합 초기화 | ||
| let currentSum = nums[0]; | ||
| let maxSum = nums[0]; | ||
|
|
||
| // 두 번째 요소부터 순회 | ||
| for (let i = 1; i < nums.length; i++) { | ||
| // 현재 위치에서의 최대 부분 배열 합 계산 | ||
| // "이전까지의 합 + 현재 요소" vs "현재 요소부터 새로 시작" | ||
| currentSum = Math.max(nums[i], currentSum + nums[i]); | ||
|
|
||
| // 전체 최대값 업데이트 | ||
| maxSum = Math.max(maxSum, currentSum); | ||
| } | ||
|
|
||
| return maxSum; | ||
| } |
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,31 @@ | ||
| /** | ||
| * | ||
| * @param n | ||
| * | ||
| * 풀이 1 | ||
| * | ||
| * function hammingWeight(n: number): number { | ||
| * let parse = (n).toString(2); | ||
| * let count = 0; | ||
| * for (const item of parse){ | ||
| * if(+item ===1) count ++ | ||
| * } | ||
| * return count | ||
| * }; | ||
| * | ||
| * 숫자를 문자열로 변환할때 오버헤드 발생 | ||
| * 비트 연산을 사용해야할듯! | ||
| * | ||
| * 검색해보니 Brian Kernighan 알고리즘을 사용하면 더 효율적이라고 한다 | ||
| */ | ||
|
|
||
| function hammingWeight(n: number): number { | ||
| let count = 0; | ||
|
|
||
| while (n !== 0) { | ||
| n &= (n - 1); | ||
| count++; | ||
| } | ||
|
|
||
| return count; | ||
| } |
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,34 @@ | ||
| /** | ||
| * | ||
| * @param s | ||
| * | ||
| * 풀이 1 | ||
| * | ||
| * 특수문자 정규 표현식이 복잡하고, 분할과 합치는 과정이 중복된다 | ||
| * | ||
| * function isPalindrome(s: string): boolean { | ||
| * const reg= /[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>@\#$%&\\\=\(\'\"]/gi; | ||
| * let palindrome= s.replace(reg,'').toLowerCase().split(''); | ||
| * | ||
| * return palindrome.join('').replace(/ /g,"")===palindrome.reverse().join('').replace(/ /g,"") | ||
| * }; | ||
| * | ||
| * 그래서 생각한 풀이 2는 s consists only of printable ASCII characters. 을 보고 숫자와 알파벳을 제외하고 나머지는 제거하고 | ||
| * 투포인트 방법으로 변경해서 문제 해결 | ||
| */ | ||
|
|
||
| function isPalindrome(s: string): boolean { | ||
| const cleanStr = s.toLowerCase().replace(/[^a-z0-9]/g, ''); | ||
|
|
||
| let left = 0; | ||
| let right = cleanStr.length-1; | ||
|
|
||
| while(left < right){ | ||
| if(cleanStr[left] !== cleanStr[right]){ | ||
| return false; | ||
| } | ||
| left++; | ||
| right--; | ||
| } | ||
| return true | ||
| }; |
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.
twoDigit의 경우 문자열 덧셈 후 parseInt 하는 방식보다는 슬라이싱 후 숫자 연산으로 처리하는 방법도 가능할 것 같아요!
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.
오호 좋네요!