-
-
Notifications
You must be signed in to change notification settings - Fork 304
[sujeong-dev] WEEK 02 solutions #2079
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
+122
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bcda32e
feat: solve valid-anagram
sujeong-dev 386e0ac
feat: solve climbing-stairs
sujeong-dev f032c1d
fix: early return 추가
sujeong-dev 4953d89
feat: easy 복잡도 계산 주석 추가
sujeong-dev c487499
feat: solve array except self
sujeong-dev e52f5f9
feat: solve 3sum
sujeong-dev 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,24 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number[][]} | ||
| * | ||
| * 시간복잡도: 이중 for문으로 O(n^2)이긴한데 너무 오래걸림 | ||
| * 해설 확인해보기 | ||
| */ | ||
| var threeSum = function (nums) { | ||
| if (nums.every((num) => num === 0)) return [nums.slice(0, 3)]; | ||
|
|
||
| const threeSet = new Set(); | ||
| for (let i = 0; i < nums.length - 2; i++) { | ||
| const twoSet = new Set(); | ||
| for (let j = i + 1; j < nums.length; j++) { | ||
| const findNum = -(nums[i] + nums[j]); | ||
| if (twoSet.has(findNum)) { | ||
| const triplet = [nums[i], nums[j], findNum].sort((a, b) => a - b); | ||
| threeSet.add(triplet.join(',')); | ||
| } | ||
| twoSet.add(nums[j]); | ||
| } | ||
| } | ||
| return Array.from(threeSet, (k) => k.split(',').map(Number)); | ||
| }; |
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 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| * | ||
| * 시간복잡도 계산 | ||
| * 메모이제이션으로 n번째에는 n번째에 대한 부분만 연산되므로 O(n) | ||
| * | ||
| * 공간복잡도 계산 | ||
| * memo배열에 n번째 연산값들이 각 인덱스에 할당되므로 O(n) | ||
| d */ | ||
|
|
||
| var climbStairs = function (n, memo = []) { | ||
| if (memo[n] !== undefined) return memo[n]; | ||
|
|
||
| if (n <= 1) return 1; | ||
|
|
||
| memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo); | ||
|
|
||
| return memo[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,48 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number[]} | ||
|
|
||
| result[i] = (nums[0] * ... * nums[i-1]) * (nums[i+1] * nums[n-1]) | ||
| i=0 r[0]= 1 | ||
| i=1 r[1]= 1 * nums[0] = 1 | ||
| i=2 r[2]= 1 * nums[0] * nums[1] = 2 | ||
| i=3 r[3]= 1 * nums[0] * nums[1] * nums[2] = 6 | ||
|
|
||
| i=0 r[0]=24 | ||
| i=1 r[1]= 1 * nums[3] * nums[2] = 12 | ||
| r=2 r[2]= 1 * nums[3] =4 | ||
| r=3 r[3]= 1 | ||
|
|
||
| i=0 r[0]=1 * 24 | ||
| i=1 r[1]=1 * 12 | ||
| i=2 r[2]=2 * 4 | ||
| i=3 r[3]=6 * 1 | ||
| 인덱스를 중심으로 왼쪽 구간 원소의 곱 * 오른쪽 구간 원소의 곱 | ||
|
|
||
| 시간복잡도 계산 | ||
| 왼쪽 구간 for문 nums에 비례해서 연산 | ||
| 오른쪽 구간 for문 nums에 비례해서 연산 | ||
| => O(n) | ||
|
|
||
| 공간복잡도 계산 | ||
| start, end 할당(출력공간인 resultArray배열은 추가공간에서 제외된다.) | ||
| => O(1) | ||
| */ | ||
| var productExceptSelf = function (nums) { | ||
| let resultArray = Array(nums.length).fill(1); | ||
| const n = nums.length; | ||
|
|
||
| let start = 1; | ||
| for (let i = 1; i < n; i++) { | ||
| start *= nums[i - 1]; | ||
| resultArray[i] = start; | ||
| } | ||
|
|
||
| let end = 1; | ||
| for (let i = n - 2; i >= 0; i--) { | ||
| end *= nums[i + 1]; | ||
| resultArray[i] *= end; | ||
| } | ||
|
|
||
| return resultArray; | ||
| }; |
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 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @param {string} t | ||
| * @return {boolean} | ||
| * | ||
| * 시간복잡도 계산 | ||
| * 문자열 s 길이만큼 Map에 set => n | ||
| * 문자열 t 길이만큼 Map에 set => n | ||
| * 따라서 O(n) | ||
| * | ||
| * 공간복잡도 계산 | ||
| * 문자열 s, t의 길이만큼 Map에 할당되니까 O(n) | ||
| */ | ||
| var isAnagram = function (s, t) { | ||
| if (s.length !== t.length) return false; | ||
|
|
||
| const frequencyMap = new Map(); | ||
|
|
||
| for (const x of s) { | ||
| frequencyMap.set(x, (frequencyMap.get(x) || 0) + 1); | ||
| } | ||
|
|
||
| for (const y of t) { | ||
| if (!frequencyMap.has(y)) return false; | ||
| frequencyMap.set(y, frequencyMap.get(y) - 1); | ||
| if (frequencyMap.get(y) === 0) frequencyMap.delete(y); | ||
| } | ||
|
|
||
| return frequencyMap.size === 0; | ||
| }; |
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.
안녕하세요! 풀이 확인 했습니다😀
변수명을 start, end 대신 leftProduct, rightProduct 이런 식으로 왼쪽, 오른쪽을 명시해주는 방식도 생각이 납니다!