Skip to content

Commit 795516e

Browse files
committed
feat 0703. Kth Largest Element in a Stream temp
1 parent 3190ec8 commit 795516e

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
// 時間複雜 O(), 空間複雜 O()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
arg1 string
7+
want int
8+
}{
9+
{
10+
"bbbab",
11+
4,
12+
},
13+
}
14+
15+
func TestLongestPalindromeSubseq(t *testing.T) {
16+
for _, tt := range tests {
17+
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
18+
if got := LongestPalindromeSubseq(tt.arg1); got != tt.want {
19+
t.Errorf("got = %v, want = %v", got, tt.want)
20+
}
21+
}
22+
}
23+
24+
func BenchmarkLongestPalindromeSubseq(b *testing.B) {
25+
b.ResetTimer()
26+
for i := 0; i < b.N; i++ {
27+
LongestPalindromeSubseq(tests[0].arg1)
28+
}
29+
}
30+
31+
/*
32+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=.
33+
34+
*/

0 commit comments

Comments
 (0)