-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Sehwan] Week3 solutions with javascript #80
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
Conversation
}; | ||
|
||
// TC: O(n) | ||
// SC: O(n) |
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) 로도 가능한데 도전해보셔도 좋을 것 같습니다 : )
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(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)으로 만드는게 가능하더라고요!
// Initialize the first two steps
let first = 1;
let second = 2;
// Iterate to compute the number of ways to reach step n
for (let i = 3; i <= n; i++) {
let current = first + second;
first = second;
second = current;
}
피드백 감사드립니다!! :^)
let queue = [root]; | ||
let depth = 0; | ||
// Iterate until there is an element inside of queue | ||
while (queue.length > 0) { | ||
// Record level size to avoid fault size measuring | ||
let levelSize = queue.length; |
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.
안녕하세요 세환님!
for 문 안에 node는 const keyword로 선언된 데 반해, queue랑 levelSize는 let로 선언한 이유가 따로 있을까요?
저 같은 경우는 처음에 변수를 선언할 때 먼저 const keyword로 선언하고 이후에 재할당 된다면 선언을 let으로 변경하는데요.
그렇게 행동하는 편이 나중에 코드를 읽을 때 해당 변수가 어떻게 조작될지 예측할 수 있고, 선언 이후에 실수로라도 의도치 않은 재할당을 방지할 수 있는 이득이 있기 때문이에요.
시간 나신다면, 혹시 아래 suggestion 참고해주셨으면 좋겠습니다!
let queue = [root]; | |
let depth = 0; | |
// Iterate until there is an element inside of queue | |
while (queue.length > 0) { | |
// Record level size to avoid fault size measuring | |
let levelSize = queue.length; | |
const queue = [root]; | |
let depth = 0; | |
// Iterate until there is an element inside of queue | |
while (queue.length > 0) { | |
// Record level size to avoid fault size measuring | |
const levelSize = queue.length; |
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.
안녕하세요 Evan님!
네 코맨트 주신대로 queue와 levelSize는 const로 선언하는게 좋은 접근방법인것 같아요.
저도 실제 업무에는 const로 선언하고 시작하는 편인데 알고리즘 풀이할 땐 습관적으로 let을 과도하게 썼던 것 같습니다. 🤦🏻♂️
좋은 피드백 감사드려요!
고생하셨습니다~! |
No description provided.