-
-
Notifications
You must be signed in to change notification settings - Fork 304
[robinyoon-dev] WEEK 02 solutions #2035
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| */ | ||
| var climbStairs = function(n) { | ||
| //(n) = f(n - 1) + f(n - 2) | ||
| let tempArray = []; | ||
|
|
||
| for(let i = 0; i <= n; i++){ | ||
| if(i === 0 || i === 1){ | ||
| tempArray.push(1); | ||
| }else{ | ||
| let tempSum = 0; | ||
| tempSum = tempArray[i - 2] + tempArray[i - 1]; | ||
| tempArray.push(tempSum); | ||
| } | ||
| } | ||
| return tempArray[n]; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number[]} | ||
| */ | ||
| var productExceptSelf = function (nums) { | ||
|
|
||
| const NUMS_LENGTH = nums.length; | ||
| const isZeroArray = []; //boolean | ||
|
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. O(1) extra space를 사용하라는 follow up 조건을 위해서 for 문 내에서 검사할 때는 |
||
| let zeroCount = 0; | ||
|
|
||
| let totalProduct = nums.reduce((acc, item) => { | ||
| if (item === 0) { | ||
| zeroCount++; | ||
| isZeroArray.push(true); | ||
| return acc; | ||
| } else { | ||
| isZeroArray.push(false); | ||
| return acc * item; | ||
| } | ||
| }, 1); | ||
|
|
||
| // 엣지 케이스 대비 1: nums의 요소 중 0이 두 개 이상인 경우 | ||
| if (zeroCount >= 2) { | ||
| totalProduct = 0; | ||
| } | ||
|
|
||
| const tempArray = []; | ||
|
|
||
| for (let i = 0; i < NUMS_LENGTH; i++) { | ||
| if (isZeroArray[i] === true) { | ||
| tempArray.push(totalProduct); | ||
| } else if (zeroCount >= 1) { | ||
| // 엣지 케이스 대비 2: isZeroArray[i]가 false 더라도 nums의 요소 중 zero가 하나라도 있는 경우 | ||
| // (지금 보니 이 부분은 zeroCount === 1로 했어도 될 것 같네요...) | ||
| tempArray.push(0); | ||
| } else { | ||
| tempArray.push(totalProduct / nums[i]); | ||
|
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. 문제의 |
||
| } | ||
| } | ||
|
|
||
| return tempArray; | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @param {string} t | ||
| * @return {boolean} | ||
| */ | ||
| var isAnagram = function (s, t) { | ||
| //1. s와 t를 Array로 만든다. | ||
| const sArray = s.split(""); | ||
| const tArray = t.split(""); | ||
|
|
||
| //2. sArray와 tArray의 sort를 같게 만든다. | ||
| const sortedSArray = sArray.sort(); | ||
| const sortedTArray = tArray.sort(); | ||
|
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. 정렬을 이용해서 풀이하셨네요! 정렬을 하게 되면 시간 복잡도가 O(nlogn)이 되는데요, 카운팅을 이용하면 시간 복잡도를 약간 더 최적화 할 수 있어 이렇게도 한 번 풀어보시는 걸 추천드려요~! |
||
|
|
||
| //3. sArray와 tArray가 같은지 판별한다. | ||
| const result = JSON.stringify(sortedSArray) === JSON.stringify(sortedTArray); | ||
|
|
||
| //4. 같으면 true를, 다르면 false를 반환한다. | ||
| return result; | ||
|
|
||
| }; | ||
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의 변수를 사용해서 공간 복잡도를 한 단계 최적화 할 수 있을 것 같아요~!