Skip to content

[선재] Week1 문제 풀이 #306

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 7 commits into from
Aug 16, 2024
Merged

[선재] Week1 문제 풀이 #306

merged 7 commits into from
Aug 16, 2024

Conversation

Sunjae95
Copy link
Member

No description provided.

@dev-jonghoonpark
Copy link
Contributor

  • Contains Duplicate
  • Number of 1Bits
    코드 잘 보았습니다!

Project와 iterator 까지 설정 부탁드립니다...!

image

@Sunjae95
Copy link
Member Author

  • Contains Duplicate
  • Number of 1Bits
    코드 잘 보았습니다!

Project와 iterator 까지 설정 부탁드립니다...!

image

organization 수락을 안해서 project가 안눌러졌네요.
확인 감사합니다:)


array.sort((a, b) => b.count - a.count);

for (let i = 0; i < k; i++) answer.push(array[i].number);
Copy link
Contributor

Choose a reason for hiding this comment

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

이렇게 구현하셔도 좋습니다. 그런데 .slice()를 사용해서 구현해도 더 짧은 코드로 같은 의도를 전달할 수 있을것 같습니다.

Copy link
Member Author

Choose a reason for hiding this comment

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

haklee님 안녕하세요 첫 리뷰어시네요 방문감사합니다 :)
리뷰내용이 answer 배열대신 array에서 값을 리턴하라는 의도로 해석되는데요.

만약에 제가 해석한 의도가 맞다면 for문을 사용한 이유는 아래와 같습니다.
slice에 데이터를 정제할 수 있는 부분이 있으면 좋겠지만 JS의 array.slice 내장 메서드는 값을 변경하는 기능이 존재하지 않습니다. slice를 사용한다면 데이터가 원시값이 아닌 객체로 이기에 데이터를 변경해야하므로 배열을 한번더 순회해야하는 불편함이 있기에 for문으로 작성하게 됐네요.

리뷰해주신 의도가 제가 답변한 내용과 맞을까요?
혹시 틀리거나 잘못된 내용있다면 확인후 코멘트 부탁드립니다 :)

Copy link
Contributor

Choose a reason for hiding this comment

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

어떤 의도셨는지는 알겠습니다! 제가 js를 아주 잘 알지는 않아서 궁금한 것이 있는데요, 혹시 slice 후 map을 사용하는 것과 k번 array에 push하는 것 중에 어떤 것이 오버헤드가 더 크게 걸릴까요? 알고리즘의 TC가 O(N logN)이라고 적어주셔서 동작에는 크게 영향을 주지 않을 것이라 중요한 질문은 아닙니다.

Copy link
Member Author

Choose a reason for hiding this comment

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

결론은 둘다 O(n)으로 동일입니다.

코멘트보고 곰곰이 생각해봤는데요. slice와 map은 별개로 동작해서 O(n) + O(n) 으로 시간복잡도가 계산되네요?
제가 method chaining 으로 인해 O(n^2)로 된다고 잘못알고있었네요.

이러면 haklee님 리뷰내용대로 array.slice().map()으로 구현하면 명시적일거 같습니다.

중요한 질문이었네요. 감사합니다 :)

@Sunjae95 Sunjae95 marked this pull request as ready for review August 14, 2024 23:59
Copy link
Member

@DaleSeo DaleSeo left a comment

Choose a reason for hiding this comment

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

깔끔하게 잘 풀어주신 것 같아요!
image

Comment on lines +13 to +14
* reason:
* javascript sort method is easier to implement.
Copy link
Member

Choose a reason for hiding this comment

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

이렇게 이유를 남겨주시니 완전 납득이 되네요 ㅋㅋㅋ
다음 주차까지 아직 시간이 많으니 brainstorming하셨던 priority queue를 사용해서도 풀어보시면 좋을 것 같습니다. 물론 현실 프로젝트에서는 간단하고 읽기 쉬운 코드가 더 선호되지만, 코딩 테스트에서는 결국 더 효율적인 알고리즘을 제시하는 것이 목표이니까요.

Copy link
Contributor

@nhistory nhistory left a comment

Choose a reason for hiding this comment

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

수고하셨습니다!

@nhistory nhistory merged commit 3780d9f into DaleStudy:main Aug 16, 2024
@Sunjae95 Sunjae95 added the js label Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

5 participants