-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Sophia] Week3 Solutions #394
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,24 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
let climbStairs = function (n) { | ||
if (n <= 1) return 1; | ||
|
||
let ways = new Array(n + 1); | ||
ways[0] = 1; | ||
ways[1] = 1; | ||
|
||
for (let i = 2; i <= n; i++) { | ||
ways[i] = ways[i - 1] + ways[i - 2]; // 점화식 사용 | ||
} | ||
|
||
return ways[n]; | ||
}; | ||
|
||
/* | ||
1. 시간 복잡도: O(n) | ||
- for 루프의 시간 복잡도 | ||
2. 공간 복잡도: O(n) | ||
- 배열 ways의 공간 복잡도 | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
let twoSum = function (nums, target) { | ||
let indices = {}; | ||
|
||
nums.forEach((item, index) => { | ||
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.
|
||
indices[item] = index; | ||
}); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
let findNum = target - nums[i]; | ||
|
||
if (indices[findNum] !== i && findNum.toString() in indices) { | ||
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 [indices[findNum], i]; | ||
} | ||
} | ||
}; | ||
|
||
/* | ||
1. 시간복잡도: O(n) | ||
- forEach와 for루프의 시간복잡도가 각 O(n) | ||
2. 공간복잡도: O(n) | ||
- indices 객체의 공간복잡도가 O(n), 나머지는 O(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.
점화식을 이용하신 부분이 좋습니다! 👍 조금 더 개선해서 공간 복잡도를
O(1)
으로 줄여보시면 어떨까요?