Skip to content

Conversation

@syoshida20
Copy link
Owner

@syoshida20 syoshida20 commented Apr 17, 2025

問題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

@syoshida20 syoshida20 self-assigned this Apr 17, 2025
@syoshida20 syoshida20 force-pushed the feature/heap-and-priority-queue/kth-largest-element-in-a-stream branch from 6a624c9 to c69ecbc Compare April 17, 2025 00:17
@syoshida20 syoshida20 marked this pull request as ready for review April 20, 2025 08:57
}
}
const KthLargest = function(k, nums) {
this.heap = new MinHeap()

Choose a reason for hiding this comment

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

top_ktop_k_heapのようにした方が意図が伝わりやすくより良いと思います。

Copy link
Owner Author

@syoshida20 syoshida20 May 6, 2025

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
Copy link

Choose a reason for hiding this comment

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

個人的には break をする条件を先に書きたいですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

IF文を書くときにはどちらを先にbreakさせるべきかを毎回検討しようと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

caedaa0 でレビューの修正前/修正後のコードを追加しました。


## STEP2

* 問題で期待されているHeapを用いてStep2以降を行う。
Copy link

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 が標準で提供されているようです。

https://support.leetcode.com/hc/en-us/articles/360011833974-What-are-the-environments-for-the-programming-languages#:~:text=NET%209%20runtime-,JavaScript,-node.js%2022.14.0

Copy link
Owner Author

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すれば良いというのが直感で理解できない。
Copy link

Choose a reason for hiding this comment

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

それは関数名との関係ですね。siftUp はある位置の値が変化したときに木の整合性を取るメソッドです。
まず、下との整合性を取り、その後で上との整合性を取ります。
半分の要素で siftUp を呼べば一番下の段はもう整合性を取り終わっているんですね。

Copy link
Owner Author

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を呼ぶことで、
一番下の段もヒープ条件が満たされるということが納得できました。

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants