-
-
Notifications
You must be signed in to change notification settings - Fork 304
[hozzijeong] WEEK 02 solutions #2043
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
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,45 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number[][]} | ||
| */ | ||
| var threeSum = function(nums) { | ||
| nums.sort((a,b) => a-b); | ||
| const length = nums.length; | ||
|
|
||
| const answer = []; | ||
|
|
||
|
|
||
| for(let i = 0; i < length - 2; i++){ | ||
| if(i > 0 && nums[i] === nums[i-1]) continue; | ||
| if(nums[i] > 0) break; | ||
|
|
||
| let left = i + 1; | ||
| let right = length -1; | ||
|
|
||
| while(left < right){ | ||
| const result = nums[left]+ nums[right] + nums[i]; | ||
|
|
||
| if(result > 0) { | ||
| right -= 1; | ||
| } | ||
|
|
||
| if(result < 0) { | ||
| left +=1; | ||
| } | ||
|
|
||
| if(result === 0) { | ||
| answer.push([nums[i],nums[left],nums[right]]); | ||
|
|
||
| while(left < right && nums[left] === nums[left + 1]) left += 1; | ||
| while(left < right && nums[right] === nums[right - 1]) right -=1; | ||
|
|
||
| left += 1; | ||
| right -= 1; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| return answer | ||
| }; |
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,21 @@ | ||
| /** | ||
| * DP 문제라는 것을 알았지만 개념을 잘 몰라서 약간의 검색을 했습니다. | ||
| * 하나의 문제를 여러 작은 문제들로 쪼갤 수 있고, 그 쪼갠 문제가 다시 해당 문제의 해답이 될 수 있으며, 중복된 값이 존재하는 조건에 맞기 떄문에 DP로 풀었습니다. | ||
| * 처음에는 피보나치 형식으로 접근했다가 시간초과가 나버려서 bottom-up 방식으로 변경해서 적용했습니다 | ||
| */ | ||
|
|
||
|
|
||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| */ | ||
| var climbStairs = function(n) { | ||
| const answer = [1,2]; | ||
|
|
||
| for(let i = 2; i < n; i++){ | ||
| const value = answer[i-1] + answer[i-2]; | ||
| answer.push(value); | ||
| } | ||
|
|
||
| return answer[n-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,23 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number[]} | ||
| */ | ||
| var productExceptSelf = function(nums) { | ||
| // 0이 2개 이상인 경우에는 무조건 0 | ||
| if(nums.filter((num) => num === 0).length > 1) return Array.from({length:nums.length}).fill(0); | ||
|
|
||
| // 0이 1개인 경우에 나타나는 수 | ||
| const hasZero = nums.includes(0); | ||
|
|
||
| const allMatrix = nums.filter(Boolean). reduce((acc, cur) => (acc * cur),1); | ||
|
|
||
| return nums.map((num) => { | ||
| if(hasZero){ | ||
| if(num !== 0) return 0; | ||
|
|
||
| return allMatrix | ||
| } | ||
|
|
||
| return allMatrix / num | ||
|
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. 문제의 |
||
| }); | ||
| }; | ||
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 {string} s | ||
| * @param {string} t | ||
| * @return {boolean} | ||
| */ | ||
|
|
||
| /** | ||
| * map을 통해 문자의 개수를 저장하고, 두 문자열의 문자 개수를 비교해서 다른 값이 있다면 false를 반환하도록 풀이를작성했습니다 | ||
| * | ||
| */ | ||
| var isAnagram = function(s, t) { | ||
| if(s.length !== t.length) return false; | ||
|
|
||
| const length = s.length; | ||
|
|
||
| const stringMap = new Map(); | ||
| const targetMap = new Map(); | ||
|
|
||
| for(let i = 0; i < length; i ++){ | ||
|
|
||
| const stringChar = s[i]; | ||
| const currentStringMap = stringMap.get(stringChar); | ||
|
|
||
| if(currentStringMap){ | ||
| stringMap.set(stringChar, currentStringMap + 1) | ||
| }else{ | ||
| stringMap.set(stringChar, 1) | ||
| } | ||
|
|
||
| const targetChar = t[i]; | ||
| const currentTargetMap = targetMap.get(targetChar); | ||
|
|
||
| if(currentTargetMap){ | ||
| targetMap.set(targetChar, currentTargetMap + 1) | ||
| }else{ | ||
| targetMap.set(targetChar, 1) | ||
| } | ||
| } | ||
|
|
||
| for(const [char, count] of [...stringMap]){ | ||
| const targetCount = targetMap.get(char); | ||
|
|
||
| if(!targetCount) return false; | ||
| if(targetCount !== count) 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.
이렇게 DP 테이블의 가장 최근 n개의 원소만 사용하는 경우에는 O(n) space의 DP 테이블 대신 O(1)space의 변수를 사용해서 공간 복잡도를 한 단계 최적화 할 수 있을 것 같아요~!