|
| 1 | +--- |
| 2 | +title: 0703. Kth Largest Element in a Stream |
| 3 | +subtitle: "https://leetcode.com/problems/kth-largest-element-in-a-stream/" |
| 4 | +date: 2023-01-22T21:20:00+08:00 |
| 5 | +lastmod: 2023-01-22T21:20:00+08:00 |
| 6 | +draft: false |
| 7 | +author: "Kimi.Tsai" |
| 8 | +authorLink: "https://kimi0230.github.io/" |
| 9 | +description: "0703.Kth-Largest-Element-in-a-Stream" |
| 10 | +license: "" |
| 11 | +images: [] |
| 12 | + |
| 13 | +tags: [LeetCode, Go, Easy, Kth Largest Element in a Stream] |
| 14 | +categories: [LeetCode] |
| 15 | + |
| 16 | +featuredImage: "" |
| 17 | +featuredImagePreview: "" |
| 18 | + |
| 19 | +hiddenFromHomePage: false |
| 20 | +hiddenFromSearch: false |
| 21 | +twemoji: false |
| 22 | +lightgallery: true |
| 23 | +ruby: true |
| 24 | +fraction: true |
| 25 | +fontawesome: true |
| 26 | +linkToMarkdown: false |
| 27 | +rssFullText: false |
| 28 | + |
| 29 | +toc: |
| 30 | + enable: true |
| 31 | + auto: true |
| 32 | +code: |
| 33 | + copy: true |
| 34 | + maxShownLines: 200 |
| 35 | +math: |
| 36 | + enable: false |
| 37 | + # ... |
| 38 | +mapbox: |
| 39 | + # ... |
| 40 | +share: |
| 41 | + enable: true |
| 42 | + # ... |
| 43 | +comment: |
| 44 | + enable: true |
| 45 | + # ... |
| 46 | +library: |
| 47 | + css: |
| 48 | + # someCSS = "some.css" |
| 49 | + # located in "assets/" |
| 50 | + # Or |
| 51 | + # someCSS = "https://cdn.example.com/some.css" |
| 52 | + js: |
| 53 | + # someJS = "some.js" |
| 54 | + # located in "assets/" |
| 55 | + # Or |
| 56 | + # someJS = "https://cdn.example.com/some.js" |
| 57 | +seo: |
| 58 | + images: [] |
| 59 | + # ... |
| 60 | +--- |
| 61 | +# [0703. Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) |
| 62 | + |
| 63 | +## 題目 |
| 64 | +Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element. |
| 65 | + |
| 66 | +Implement KthLargest class: |
| 67 | + |
| 68 | +KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums. |
| 69 | +int add(int val) Appends the integer val to the stream and returns the element representing the kth largest element in the stream. |
| 70 | + |
| 71 | + |
| 72 | +Example 1: |
| 73 | + |
| 74 | +Input |
| 75 | +["KthLargest", "add", "add", "add", "add", "add"] |
| 76 | +[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]] |
| 77 | +Output |
| 78 | +[null, 4, 5, 5, 8, 8] |
| 79 | + |
| 80 | +Explanation |
| 81 | +KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]); |
| 82 | +kthLargest.add(3); // return 4 |
| 83 | +kthLargest.add(5); // return 5 |
| 84 | +kthLargest.add(10); // return 5 |
| 85 | +kthLargest.add(9); // return 8 |
| 86 | +kthLargest.add(4); // return 8 |
| 87 | + |
| 88 | + |
| 89 | +Constraints: |
| 90 | + |
| 91 | +1 <= k <= 104 |
| 92 | +0 <= nums.length <= 104 |
| 93 | +-104 <= nums[i] <= 104 |
| 94 | +-104 <= val <= 104 |
| 95 | +At most 104 calls will be made to add. |
| 96 | +It is guaranteed that there will be at least k elements in the array when you search for the kth element. |
| 97 | +Accepted |
| 98 | +479.1K |
| 99 | +Submissions |
| 100 | +846.4K |
| 101 | +Acceptance Rate |
| 102 | +56.6% |
| 103 | + |
| 104 | +## 題目大意 |
| 105 | +設計一個找到數據流中第 k 大元素的類(class)。 注意是排序後的第 k 大元素,不是第 k 個不同的元素。 請實現 KthLargest 類: |
| 106 | +* KthLargest(int k, int[] nums) 使用整數 k 和整數流 nums 初始化物件。 |
| 107 | +* int add(int val) 將 val 插入數據流 nums 後,返回當前數據流中第 k 大的元素。 |
| 108 | + |
| 109 | +## 解題思路 |
| 110 | +這題考察優先順序佇列的使用,可以先做下這道類似的題目 [215.陣列中的第 K 個最大元素](../0215.Kth-Largest-Element-in-an-Array/README.md)。 |
| 111 | + |
| 112 | +## Big O |
| 113 | +時間複雜 : `` |
| 114 | +空間複雜 : `` |
| 115 | + |
| 116 | +## 來源 |
| 117 | +* https://leetcode.com/problems/kth-largest-element-in-a-stream/ |
| 118 | +* https://leetcode.cn/problems/kth-largest-element-in-a-stream/ |
| 119 | + |
| 120 | +## 解答 |
| 121 | +https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0703.Kth-Largest-Element-in-a-Stream/main.go |
| 122 | + |
| 123 | +```go |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +## Benchmark |
| 128 | + |
| 129 | +```sh |
| 130 | + |
| 131 | +``` |
0 commit comments