Skip to content
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

[Sehwan]Week5 solutions with javascript #98

Merged
merged 4 commits into from
May 31, 2024
Merged

Conversation

nhistory
Copy link
Contributor

@nhistory nhistory commented May 27, 2024

  • Top K Frequent Elements

  • Encode and Decode Strings

  • Product of Array Except Self (added 5/27)

  • Longest Consecutive Sequence (added 5/28)

  • 3sum (added 5/30)

@DaleSeo
Copy link
Contributor

DaleSeo commented May 28, 2024

@nhistory 님, 다른 분들처럼 문제를 다 푸실 때 까지는 Draft PR로 해주시면 좋을 것 같은데요?

}

// 3. Sort frequency and return sliced array
return [...Object.keys(map)].sort((a, b) => map[b] - map[a]).slice(0, k);
Copy link
Contributor

@DaleSeo DaleSeo May 28, 2024

Choose a reason for hiding this comment

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

Cool! 😝

@nhistory nhistory marked this pull request as draft May 28, 2024 01:15
@nhistory
Copy link
Contributor Author

@nhistory 님, 다른 분들처럼 문제를 다 푸실 때 까지는 Draft PR로 해주시면 좋을 것 같은데요?

수정했습니다!

@nhistory nhistory marked this pull request as ready for review May 28, 2024 02:13
@nhistory nhistory marked this pull request as draft May 28, 2024 02:13
@@ -0,0 +1,30 @@
var longestConsecutive = function (nums) {
// Return 0 if there are no elements in nums
if (nums.length === 0) return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 이렇게 앞에 예외 처리해주는 걸 생각하지 않았는데 이렇게 미리 걸러내주는게 효율적이겠군요.

}

// 3. Sort frequency and return sliced array
return [...Object.keys(map)].sort((a, b) => map[b] - map[a]).slice(0, k);
Copy link
Contributor

Choose a reason for hiding this comment

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

object 앞에 ...은 뭘 의미할까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

자바스크립트에서 '...'은 spread operator로 context에 따라 다양하게 사용되곤 하는데요.
여기서는 map 오브젝트의 key들을 어레이로 받을 때 shallow copy를 하기 위해서 사용했습니다.
spread operator 없이 아래와 같은 코드로 실행해도 문제는 없는데,

return (Object.keys(map).sort((a,b)=>map[b]-map[a]).slice(0,k))

원본 오브젝트를 변경하지 않고 리턴값을 받아야 하는 상황을 가정했다고 보시면 될 것 같습니다.

Copy link
Contributor

@SamTheKorean SamTheKorean May 30, 2024

Choose a reason for hiding this comment

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

더 안전하게 실행하려고 사용하신 거군요~! 친절한 설명감사드립니다! 덕분에 자바스크립트도 배웁니다!

@nhistory nhistory marked this pull request as ready for review May 31, 2024 00:32
Copy link
Contributor

@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.

수고하셨어용!

@DaleSeo DaleSeo merged commit 27dbd40 into DaleStudy:main May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants