Skip to content

[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

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions climbing-stairs/seona926.js
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]; // 점화식 사용
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

점화식을 이용하신 부분이 좋습니다! 👍 조금 더 개선해서 공간 복잡도를 O(1) 으로 줄여보시면 어떨까요?

}

return ways[n];
};

/*
1. 시간 복잡도: O(n)
- for 루프의 시간 복잡도
2. 공간 복잡도: O(n)
- 배열 ways의 공간 복잡도
*/
27 changes: 27 additions & 0 deletions two-sum/seona926.js
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 line 의 조건을 만족하지 않는 경우, 즉, 필요한 경우에만 indices를 추가해준다면 순회 1회를 줄일 수 있지 않을까요?

indices[item] = index;
});

for (let i = 0; i < nums.length; i++) {
let findNum = target - nums[i];

if (indices[findNum] !== i && findNum.toString() in indices) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findNum.toString() 은 아마 object의 key로 number를 사용하는 부분을 신경 써 주신것 같은데 맞을까요?
이런 목적이 맞으시다면, Map을 사용하는것도 좋지 않을까 싶습니다! 의도적으로 object를 사용하셨다면 무시하셔도 좋습니다!

return [indices[findNum], i];
}
}
};

/*
1. 시간복잡도: O(n)
- forEach와 for루프의 시간복잡도가 각 O(n)
2. 공간복잡도: O(n)
- indices 객체의 공간복잡도가 O(n), 나머지는 O(1)
*/