Skip to content

[01-binary] WEEK 03 solutions#2457

Open
01-binary wants to merge 2 commits intoDaleStudy:mainfrom
01-binary:main
Open

[01-binary] WEEK 03 solutions#2457
01-binary wants to merge 2 commits intoDaleStudy:mainfrom
01-binary:main

Conversation

@01-binary
Copy link
Contributor

@01-binary 01-binary commented Mar 20, 2026

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

Copy link
Contributor

@dohyeon2 dohyeon2 left a comment

Choose a reason for hiding this comment

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

지난 주 지나치신 문제들도 포함해서 풀어주신 것 같네요.
8문제나 푸시느라 고생하셨습니다!
시간복잡도와 공간복잡도도 추측해보시고 남겨주시면 공부가 더 될 것 같습니다!
다음 주도 파이팅입니다!

Comment on lines +7 to +8
if (n === 1) return 1;
if (n === 2) return 2;
Copy link
Contributor

Choose a reason for hiding this comment

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

if (n <= 2) return n;

으로 간결하게 표현 가능할 것 같아요.

var climbStairs = function (n) {
if (n === 1) return 1;
if (n === 2) return 2;
const arr = [0, 1, 2];
Copy link
Contributor

Choose a reason for hiding this comment

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

배열을 활용하는 방법도 괜찮지만, 3개의 변수로 설정하는게 나을 것 같습니다.
JS에서 배열이 할당되면 내부적으로 여분 메모리를 두는 것으로 알고 있어서요.
3개의 변수로 설정하는 것 보다 성능적으로 불리할 것 같네요.

if (n === 2) return 2;
const arr = [0, 1, 2];
let i = 3;
while (i <= n) {
Copy link
Contributor

Choose a reason for hiding this comment

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

현재도 의도대로 작동하지만, for문으로 작성하는 게 가독성과 안전 측면에서 더 좋을 것 같습니다.

return dfs(nums, 0, memo);
};

const dfs = (nums, i, memo) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

보통 dfs 함수 선언 자체를 함수 안에 선언하여 클로져 스코프로 변수를 참조하고는 하는데,
memo와 nums를 인자로 전달하여 변수 스코프를 제어한 부분이 인상깊습니다.
조금 더 안정적인 인상을 주네요.

* @return {number}
*/
var longestConsecutive = function (nums) {
const unique_arr = Array.from(new Set(nums));
Copy link
Contributor

Choose a reason for hiding this comment

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

JS에서는 카멜케이스를 쓰는 경우가 많아서
통일이 되면 더 좋을 것 같습니다.

Copy link
Contributor

Choose a reason for hiding this comment

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

참고

지금 접근도 좋습니다만,
보다 다양한 방법이 있어서
제 풀이에 다양한 연구를 주석으로 달아 놓았으니,
참고하시면 공부가 될 것 같습니다!

const l = Array.from(nums.length);
const a = Array.from(nums.length);
for (let i = 0; i < nums.length; i++) {
const temp = r[i - 1] ?? 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

기왕 위에서 초기화 해주는 김에
fill로 1을 할당하도록 하면, ?? 연산자를 사용해서 fallback을 관리하는 부담이 줄어들 것 같습니다.

r.fill(1);

const l = Array.from(nums.length);
const a = Array.from(nums.length);
for (let i = 0; i < nums.length; i++) {
const temp = r[i - 1] ?? 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

i - 1로 조회하는 부분이 있으니,

for (let i = 1; ...

위와 같이 1부터 조회하면 안전할 것 같습니다.

const temp2 = nums[i - 1] ?? 1;
r[i] = temp * temp2;
}
for (let i = nums.length - 1; i > -1; i--) {
Copy link
Contributor

Choose a reason for hiding this comment

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

이 부분도 마찬가지로

nums.length - 2;

로 설정하면 안전할 것 같습니다.


const iter = sortedMap.keys();
const result = [];
for (let i = 0; i < k; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Array.from(sortedMap.keys()).slice(0, k);

로 단순화 할 수 있을 것 같습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

2 participants