-
Notifications
You must be signed in to change notification settings - Fork 0
703. Kth Largest Element in a Stream #13
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
base: main
Are you sure you want to change the base?
703. Kth Largest Element in a Stream #13
Conversation
6a624c9 to
c69ecbc
Compare
| } | ||
| } | ||
| const KthLargest = function(k, nums) { | ||
| this.heap = new MinHeap() |
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.
top_kやtop_k_heapのようにした方が意図が伝わりやすくより良いと思います。
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.
こちらで同じ指摘をodaさんがされていました。
どのように使うかを意識して変数名を決めようと思います。
https://discord.com/channels/1084280443945353267/1337068598337736838/1338007445892763732
mp は変数名が何を意味しているかのパズルになるので、そこから下でどう使うかの助けとなるような名前にするといいでしょう。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.ido8j91sj503これは日常でも同じで多くのものを「食卓」「会議室」など使用目的で呼称していませんか。
| this.nums[i+1] =this.nums[i] | ||
| this.nums[i] = tmp | ||
| } else { | ||
| break |
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.
個人的には break をする条件を先に書きたいですね。
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.
IF文を書くときにはどちらを先にbreakさせるべきかを毎回検討しようと思います。
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.
caedaa0 でレビューの修正前/修正後のコードを追加しました。
|
|
||
| ## STEP2 | ||
|
|
||
| * 問題で期待されているHeapを用いてStep2以降を行う。 |
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.
JavaScript に標準で Heap はないですが、LeetCode 環境では datastructures-js/priority-queue が標準で提供されているようです。
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.
ありがとうございます。こちら調べられておりませんでした。
言語がデータ構造に対応していない際に、LeetCodeが用意したデータ構造があるかを調べるようにします。
|
|
||
| * fhiyo https://github.com/fhiyo/leetcode/pull/10/ | ||
| * index > 0 は hasParentという関数に切り出しても良い | ||
| * heapifyが半分の要素のみsiftUpすれば良いというのが直感で理解できない。 |
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.
それは関数名との関係ですね。siftUp はある位置の値が変化したときに木の整合性を取るメソッドです。
まず、下との整合性を取り、その後で上との整合性を取ります。
半分の要素で siftUp を呼べば一番下の段はもう整合性を取り終わっているんですね。
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.
下との整合性を取り
一番下の高さ2のサブツリーがヒープ条件を満たす。
次に、高さ3のサブツリーがヒープ条件を満たす。
次に、高さ4の ...
という形で、下からヒープ条件を満たしていくと理解しました。
一番下の段のノードに対してsiftUpを呼ばなくても、2番目に下の段でsiftUpを呼ぶことで、
一番下の段もヒープ条件が満たされるということが納得できました。
問題URL
https://leetcode.com/problems/kth-largest-element-in-a-stream/
問題文
You are part of a university admissions office and need to keep track of the kth highest test score from applicants in real-time. This helps to determine cut-off marks for interviews and admissions dynamically as new applicants submit their scores.
You are tasked to implement a class which, for a given integer k, maintains a stream of test scores and continuously returns the kth highest test score after a new score has been submitted. More specifically, we are looking for the kth highest score in the sorted list of all scores.
Implement the KthLargest class:
KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of test scores nums.
int add(int val) Adds a new test score val to the stream and returns the element representing the kth largest element in the pool of test scores so far.
Example 1:
Input:
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output: [null, 4, 5, 5, 8, 8]
Explanation:
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
Example 2:
Input:
["KthLargest", "add", "add", "add", "add"]
[[4, [7, 7, 7, 7, 8, 3]], [2], [10], [9], [9]]
Output: [null, 7, 7, 7, 8]
Explanation:
KthLargest kthLargest = new KthLargest(4, [7, 7, 7, 7, 8, 3]);
kthLargest.add(2); // return 7
kthLargest.add(10); // return 7
kthLargest.add(9); // return 7
kthLargest.add(9); // return 8