- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 195
[jeongwoo903] Week 02 solutions #1230
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
return result; | ||
}, { products: lefts, rights: 1 } | ||
).products; | ||
}; |
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.
대부분의 풀이 아이디어가 비슷해서 reduce는 생각못했는데, 이런 접근법도 있군요!
reduce와 reduceRight를 활용해서 코드를 간결하게 작성한 점이 인상적입니다.
다만 처음 보는 사람이 이해하기에는 약간 복잡할 수 있겠다는 생각도 들었습니다.
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.
제가 가끔 함수형으로 짜본다고 다음과 같이 코드를 짰는데 조금 가독성이 나쁜 코드인 것 같다고 생각하긴 했습니다..ㅎㅎ
좋은 의견 감사합니다!
} | ||
|
||
return dp[n-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.
코드 자체는 깔끔하고 효율적으로 작성되었네요 👍
DP 배열을 구성하는 스타일에는 여러 가지가 있고
현재 코드는 dp[0]
이 1번째 계단, dp[1]
이 2번째 계단을 의미하는 방식으로 작성되었습니다.
또 다른 스타일로는 인덱스와 계단 번호를 일치시키는 방법도 많이 사용되는거같아 제안사항은 아니지만 공유드립니다.
:
var climbStairs = function(n) {
const dp = [1, 1]; // dp[0]은 0번째(시작점), dp[1]은 1번째 계단
for (let i = 2; i <= n; i++) {
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
};
이런 방식으로 하면 "dp[i]는 i번째 계단에 도달하는 방법의 수"라고 직관적으로 이해할 수 있어서 코드 가독성 면에서 장점이 있습니다. 물론 이건 단지 스타일의 차이일 뿐이고, 어느 쪽이든 문제 해결에는 차이가 없습니다.
두 방식 모두 많이 사용되는 패턴인거같아 공유드립니다.
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.
로직에는 차이가 없어도 다음과 같이 바꿈으로써 설명과 가독성이 나아지는 것 같네요 ㅎㅎ
알려주셔서 감사합니다!
@jeongwoo903 확인완료하였습니다 |
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!