-
Notifications
You must be signed in to change notification settings - Fork 125
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
[gitsunmin] Week3 Solutions #400
Changes from all commits
e39c2e5
65ce8e5
788607c
e02a926
2d339cd
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,17 @@ | ||
/** | ||
* https://leetcode.com/problems/climbing-stairs | ||
* time complexity : O(n) | ||
* space complexity : O(1) | ||
*/ | ||
|
||
export const upStairs = (n: number): number => { | ||
let [l, r] = [1, 2]; | ||
for (let i = 3; i <= n; i++) [l, r] = [r, l + r]; | ||
|
||
return r; | ||
}; | ||
|
||
export function climbStairs(n: number): number { | ||
if (n <= 2) return n; | ||
return upStairs(n); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* https://leetcode.com/problems/product-of-array-except-self | ||
* time complexity : O(n) | ||
* space complexity : O(1) | ||
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을 위한 answer 배열을 위해 O(n)만큼의 공간을 소모하지 않을까요? 함수 주석에 문제 주소를 넣어주시니 리뷰하기가 정말 편리하네요. 저도 이렇게 해봐야겠습니다 ㅎㅎ 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.
@HC-kang 코딩 테스트에서는 입력/결과값에 필요한 메모리는 공간 복잡도 분석할 때 제외하는 경우가 많은 것 같습니다. 아무리 좋은 알고리즘을 써도 바꿀 수 없는 부분이니까요. 이 부분에 대해서 오해가 없도록 면접관과 의사소통하는 것도 중요하겠습니다. 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. @DaleSeo 오, 그런가요? 답변해주셔서 감사합니다!
|
||
*/ | ||
function productExceptSelf(nums: number[]): number[] { | ||
const n = nums.length; | ||
const answer = new Array(n).fill(1); | ||
|
||
let leftProduct = 1; | ||
for (let i = 0; i < n; i++) { | ||
answer[i] = leftProduct; | ||
leftProduct *= nums[i]; | ||
} | ||
|
||
let rightProduct = 1; | ||
for (let i = n - 1; i >= 0; i--) { | ||
answer[i] *= rightProduct; | ||
rightProduct *= nums[i]; | ||
} | ||
|
||
return answer; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* https://leetcode.com/problems/two-sum | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
function twoSum(nums: number[], target: number): number[] { | ||
const m = new Map<number, number>(); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
if (m.has(nums[i])) | ||
return [m.get(nums[i]), i]; | ||
m.set(target - nums[i], i); | ||
} | ||
|
||
return [-1, -1]; | ||
}; |
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.
안녕하세요 @gitsunmin 님! 고생하셨습니다!
array를 활용한 값 교환 좋네요!
그런데 두 함수의 입출력이 동일하고 단일 호출임에도 별도의 함수로 분리하신 건 각각의 함수가 하나의 태스크만 수행하도록 만들고자 하는 의도이셨을까요?
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.
저도 왜 굳이 3줄의 코드를 별도의 함수로 분리하셨는지 궁금해지네요 ㅎㅎ
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.
@HC-kang
처음에 했을 때 너무 오래 걸려서 통과를 못 했었는데, 그걸 고치는 과정에서 이런 방법을 찾게됬어요!
(@DaleSeo)
함수를 분리한 의도는 네넵 말씀하신 의도가 있긴했습니다.
사실은 분리 안 해도 상관 없긴 했는데, 함수명이 왠지 너무 강해보여서.. 바꾸고 싶었어요