-
-
Notifications
You must be signed in to change notification settings - Fork 304
[Blossssom] WEEK 02 solutions #2078
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
+177
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9e56457
valid-anagram solution
Blossssom b64fd98
add description
Blossssom cb3dbc0
climbing-stairs solution
Blossssom ac12f16
product-of-array-except-self solution
Blossssom 741936b
3sum solution
Blossssom a2d6ba3
add empty line in valid-anagram
Blossssom cd23f6a
add empty line in product-of-addray-except-self
Blossssom 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 nums - 정수 배열 | ||
| * @returns - 세 요소를 합해 0이 되는 값의 배열 | ||
| * @description | ||
| * - 투 포인터 방식 | ||
| * - 결국 유니크한 조합을 찾으며 중복을 제외하는 방향 | ||
| * - 무조건 적인 반복 줄이기가 아닌 효율적 범위 반복을 학습해야함 | ||
| * - 시간 복잡도 O(N^2) | ||
| * - 공간 복잡도 O(log N) | ||
| */ | ||
| function threeSum(nums: number[]): number[][] { | ||
| const answer: number[][] = []; | ||
| nums.sort((a, b) => a - b); | ||
|
|
||
| for (let i = 0; i < nums.length - 2; i++) { | ||
| if (i && nums[i] === nums[i - 1]) { | ||
| continue; | ||
| } | ||
|
|
||
| let left = i + 1; | ||
| let right = nums.length - 1; | ||
|
|
||
| while (left < right) { | ||
| const sum = nums[i] + nums[left] + nums[right]; | ||
|
|
||
| if (sum === 0) { | ||
| answer.push([nums[i], nums[left], nums[right]]); | ||
| while (left < right && nums[left] === nums[left + 1]) { | ||
| left++; | ||
| } | ||
| while (left < right && nums[right] === nums[right - 1]) { | ||
| right--; | ||
| } | ||
| left++; | ||
| right--; | ||
| } else if (sum < 0) { | ||
| left++; | ||
| } else { | ||
| right--; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| const nums = [-1, 0, 1, 2, -1, -4]; | ||
| threeSum(nums); | ||
|
|
||
|
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. 저는 set으로 해서 연산이 오래걸린던데 투포인터로 변경해봐야겠네요! 👍 |
||
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,45 @@ | ||
| /** | ||
| * 1 or 2 스텝 가능 | ||
| * @param n - 꼭대기 | ||
| * @returns - 꼭대기 까지 도달할 수 있는 방법 수 | ||
| * @description | ||
| * - 결국 패턴은 피보나치 | ||
| * - 시간 복잡도 O(n) | ||
| * - 공간 복잡도 O(1) | ||
| */ | ||
|
|
||
| // function climbStairs(n: number): number { | ||
| // if (n <= 2) { | ||
| // return n; | ||
| // } | ||
|
|
||
| // const dp = Array.from({ length: n + 1 }, () => 0); | ||
| // dp[1] = 1; | ||
| // dp[2] = 2; | ||
|
|
||
| // for (let i = 3; i <= n; i++) { | ||
| // dp[i] = dp[i - 1] + dp[i - 2]; | ||
| // } | ||
| // return dp[n]; | ||
| // } | ||
|
|
||
| function climbStairs(n: number): number { | ||
| if (n <= 2) { | ||
| return n; | ||
| } | ||
|
|
||
| let prevTwo = 1; | ||
| let prevOne = 2; | ||
|
|
||
| for (let i = 2; i <= n; i++) { | ||
| const current = prevTwo + prevOne; | ||
| prevTwo = prevOne; | ||
| prevOne = current; | ||
| } | ||
|
|
||
| return prevOne; | ||
| } | ||
|
|
||
| const n = 3; | ||
| climbStairs(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,36 @@ | ||
| /** | ||
| * @param nums - 정수 배열 | ||
| * @returns - nums[i]를 제외한 요소들의 곱셈 값 배열 | ||
| * @description | ||
| * - 나누기를 통한 풀이는 막힘 | ||
| * - 왼쪽, 오른쪽으로 순회하며 곱을 누적하기 | ||
| * - 첫번째 반복 - 처음 값은 자신을 제외한 누적의 전체이므로 1, 이후 부터 누적 | ||
| * - 두번째 반복 - 마지막 값 또한 자신을 제외한 첫번째 반복의 누적, 이후 부터 미리 누적한 값과 자신을 곱 | ||
| * | ||
| * @description | ||
| * - 시간 복잡도 O(n) | ||
| * - 공간 복잡도 O(n) | ||
| * - 해당 방법을 떠올리지 못해 AI의 도움을 받았는데 수학적 사고가 더 필요하다. | ||
| */ | ||
| function productExceptSelf(nums: number[]): number[] { | ||
| let prev = 1; | ||
| let next = 1; | ||
| const arr: number[] = []; | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| arr.push(prev); | ||
| prev = prev * nums[i]; | ||
| } | ||
|
|
||
| for (let j = nums.length - 1; j >= 0; j--) { | ||
| arr[j] = arr[j] * next; | ||
| next = next * nums[j]; | ||
| } | ||
|
|
||
| return arr; | ||
| } | ||
|
|
||
| const nums = [1, 2, 3, 4]; | ||
| productExceptSelf(nums); | ||
|
|
||
|
|
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,47 @@ | ||
| /** | ||
| * @param s 문자열 1 | ||
| * @param t 문자열 2 | ||
| * @returns 두 문자열의 나열을 바꿔 동일한 문자열이 나올 수 있는지 반환 | ||
| * @description | ||
| * - 1. 시간 복잡도: O(n), 공간 복잡도 O(1) | ||
| * - 2. 시간, 공간 복잡도는 같지만 1번 보다는 빠름 -> 동시처리, | ||
| */ | ||
|
|
||
| // function isAnagram(s: string, t: string): boolean { | ||
| // if(s.length !== t.length) { | ||
| // return false; | ||
| // } | ||
| // const sMap = new Map(); | ||
| // for(let i = 0; i < s.length; i++) { | ||
| // sMap.has(s[i]) ? sMap.set(s[i], sMap.get(s[i]) + 1) : sMap.set(s[i], 1); | ||
| // sMap.has(t[i]) ? sMap.set(t[i], sMap.get(t[i]) - 1) : sMap.set(t[i], -1); | ||
| // } | ||
|
|
||
| // for(const v of sMap.values()) { | ||
| // if(v) { | ||
| // return false; | ||
| // } | ||
| // } | ||
| // return true; | ||
| // } | ||
|
|
||
| function isAnagram(s: string, t: string): boolean { | ||
| if (s.length !== t.length) return false; | ||
|
|
||
| const hash: Record<string, number> = {}; | ||
|
|
||
| for (const letter of s) { | ||
| hash[letter] = (hash[letter] || 0) + 1; | ||
| } | ||
|
|
||
| for (const letter of t) { | ||
| if (hash[letter] > 0) { | ||
| hash[letter] = hash[letter] - 1; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| 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.
i가 true라는건 0보다 클 때를 칭하는것이죠?!
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.
넵 0은 falsy니까 명시하지 않고 i만 적었습니다!
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.
리뷰남겨 주셔서 감사합니다!!!!