Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Top K Frequent Elements #20
base: main
Are you sure you want to change the base?
Top K Frequent Elements #20
Changes from all commits
5623eef
51a688c
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
実際に開発をしていると、独自定義の構造体には適切な Go Doc Comment が欲しいなと思うことが多いです。面接時にコードに反映させるかどうかは状況次第だと思いますが、次のように書いておくとより親切になるだろうな、ぐらいのことが考えられているとより良いように思います: https://tip.golang.org/doc/comment#type
ちなみにちゃんと書いておくとこのような見た目で出力することが出来、便利です: https://blog.lufia.org/entry/2018/05/14/150400
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.
特に問題はないですが、この解法だけ一般にはkを超える配列を返却する可能性があるな、と思いました。
(Leetcodeの制約では It is guaranteed that the answer is unique. なので、解として間違っているという事ではありません)
※Goのmakeの3つ目の引数のキャパシティは、要素数が増えたら勝手にスケールするんですね。
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.
はい、おっしゃる通りです。なので、特に使わないメソッドについてもインターフェースを満たすように実装しています。
その分、新しい領域の確保と要素のコピーのオーバーヘッドが発生するので、kを超える配列が発生する場合にそれを避けたい場合は、ユニークな要素数(
len(countToNum)
)分のキャパシティを確保する必要があるのかと思いますが、topKの要素数よりもユニークな要素数がはるかに大きいことが予想できる場合はメモリが無駄になるので考えものですね、、