703. Kth Largest Element in a Stream Easy
1/**
2 * [703] Kth Largest Element in a Stream
3 *
4 * Design a class to find the k^th largest element in a stream. Note that it is the k^th largest element in the sorted order, not the k^th distinct element.
5 * Implement KthLargest class:
6 *
7 * KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.
8 * int add(int val) Appends the integer val to the stream and returns the element representing the k^th largest element in the stream.
9 *
10 *
11 * Example 1:
12 *
13 * Input
14 * ["KthLargest", "add", "add", "add", "add", "add"]
15 * [[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
16 * Output
17 * [null, 4, 5, 5, 8, 8]
18 * Explanation
19 * KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
20 * kthLargest.add(3); // return 4
21 * kthLargest.add(5); // return 5
22 * kthLargest.add(10); // return 5
23 * kthLargest.add(9); // return 8
24 * kthLargest.add(4); // return 8
25 *
26 *
27 * Constraints:
28 *
29 * 1 <= k <= 10^4
30 * 0 <= nums.length <= 10^4
31 * -10^4 <= nums[i] <= 10^4
32 * -10^4 <= val <= 10^4
33 * At most 10^4 calls will be made to add.
34 * It is guaranteed that there will be at least k elements in the array when you search for the k^th element.
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/kth-largest-element-in-a-stream/
40// discuss: https://leetcode.com/problems/kth-largest-element-in-a-stream/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44struct KthLargest {
45 vec![]
46 }
47
48
49/**
50 * `&self` means the method takes an immutable reference.
51 * If you need a mutable reference, change it to `&mut self` instead.
52 */
53impl KthLargest {
54
55 fn new(k: i32, nums: Vec<i32>) -> Self {
56
57 }
58
59 fn add(&self, val: i32) -> i32 {
60
61 }
62}
63
64/**
65 * Your KthLargest object will be instantiated and called as such:
66 * let obj = KthLargest::new(k, nums);
67 * let ret_1: i32 = obj.add(val);
68 */
69
70// submission codes end
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn test_703() {
78 }
79}
80
Back
© 2025 bowen.ge All Rights Reserved.