Skip to content

Commit 896e06d

Browse files
committed
feat: add solutions to lc/lcof2 problems: Kth Largest Element in a
Stream
1 parent 5250a24 commit 896e06d

File tree

10 files changed

+594
-1
lines changed

10 files changed

+594
-1
lines changed

.github/workflows/starcharts.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Starcharts
22

33
on:
44
schedule:
5-
- cron: "0 0 * * 0"
5+
- cron: "0 0/12 * * *"
66
workflow_dispatch:
77

88
jobs:

lcof2/剑指 Offer II 059. 数据流的第 K 大数值/README.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,171 @@ kthLargest.add(4); // return 8
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
小根堆存放最大的 k 个元素,那么堆顶就是第 k 大的元素。
59+
5860
<!-- tabs:start -->
5961

6062
### **Python3**
6163

6264
<!-- 这里可写当前语言的特殊实现逻辑 -->
6365

6466
```python
67+
class KthLargest:
68+
69+
def __init__(self, k: int, nums: List[int]):
70+
self.q = []
71+
self.size = k
72+
for num in nums:
73+
self.add(num)
74+
75+
def add(self, val: int) -> int:
76+
heapq.heappush(self.q, val)
77+
if len(self.q) > self.size:
78+
heapq.heappop(self.q)
79+
return self.q[0]
80+
6581

82+
# Your KthLargest object will be instantiated and called as such:
83+
# obj = KthLargest(k, nums)
84+
# param_1 = obj.add(val)
6685
```
6786

6887
### **Java**
6988

7089
<!-- 这里可写当前语言的特殊实现逻辑 -->
7190

7291
```java
92+
class KthLargest {
93+
private PriorityQueue<Integer> q;
94+
private int size;
95+
96+
public KthLargest(int k, int[] nums) {
97+
q = new PriorityQueue<>(k);
98+
size = k;
99+
for (int num : nums) {
100+
add(num);
101+
}
102+
}
103+
104+
public int add(int val) {
105+
q.offer(val);
106+
if (q.size() > size) {
107+
q.poll();
108+
}
109+
return q.peek();
110+
}
111+
}
112+
113+
/**
114+
* Your KthLargest object will be instantiated and called as such:
115+
* KthLargest obj = new KthLargest(k, nums);
116+
* int param_1 = obj.add(val);
117+
*/
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class KthLargest {
124+
public:
125+
priority_queue<int, vector<int>, greater<int>> q;
126+
int size;
127+
128+
KthLargest(int k, vector<int>& nums) {
129+
size = k;
130+
for (int num : nums) add(num);
131+
}
132+
133+
int add(int val) {
134+
q.push(val);
135+
if (q.size() > size) q.pop();
136+
return q.top();
137+
}
138+
};
139+
140+
/**
141+
* Your KthLargest object will be instantiated and called as such:
142+
* KthLargest* obj = new KthLargest(k, nums);
143+
* int param_1 = obj->add(val);
144+
*/
145+
```
73146
147+
### **Go**
148+
149+
```go
150+
type KthLargest struct {
151+
h *IntHeap
152+
k int
153+
}
154+
155+
func Constructor(k int, nums []int) KthLargest {
156+
h := &IntHeap{}
157+
heap.Init(h)
158+
for _, v := range nums {
159+
heap.Push(h, v)
160+
}
161+
162+
for h.Len() > k {
163+
heap.Pop(h)
164+
}
165+
166+
return KthLargest{
167+
h: h,
168+
k: k,
169+
}
170+
}
171+
172+
func (this *KthLargest) Add(val int) int {
173+
heap.Push(this.h, val)
174+
for this.h.Len() > this.k {
175+
heap.Pop(this.h)
176+
}
177+
178+
return this.h.Top()
179+
}
180+
181+
func connectSticks(sticks []int) int {
182+
h := IntHeap(sticks)
183+
heap.Init(&h)
184+
res := 0
185+
for h.Len() > 1 {
186+
val := heap.Pop(&h).(int)
187+
val += heap.Pop(&h).(int)
188+
res += val
189+
heap.Push(&h, val)
190+
}
191+
return res
192+
}
193+
194+
type IntHeap []int
195+
196+
func (h IntHeap) Len() int { return len(h) }
197+
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
198+
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
199+
func (h *IntHeap) Push(x interface{}) {
200+
*h = append(*h, x.(int))
201+
}
202+
func (h *IntHeap) Pop() interface{} {
203+
old := *h
204+
n := len(old)
205+
x := old[n-1]
206+
*h = old[0 : n-1]
207+
return x
208+
}
209+
210+
func (h *IntHeap) Top() int {
211+
if (*h).Len() == 0 {
212+
return 0
213+
}
214+
215+
return (*h)[0]
216+
}
217+
218+
/**
219+
* Your KthLargest object will be instantiated and called as such:
220+
* obj := Constructor(k, nums);
221+
* param_1 := obj.Add(val);
222+
*/
74223
```
75224

76225
### **...**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class KthLargest {
2+
public:
3+
priority_queue<int, vector<int>, greater<int>> q;
4+
int size;
5+
6+
KthLargest(int k, vector<int>& nums) {
7+
size = k;
8+
for (int num : nums) add(num);
9+
}
10+
11+
int add(int val) {
12+
q.push(val);
13+
if (q.size() > size) q.pop();
14+
return q.top();
15+
}
16+
};
17+
18+
/**
19+
* Your KthLargest object will be instantiated and called as such:
20+
* KthLargest* obj = new KthLargest(k, nums);
21+
* int param_1 = obj->add(val);
22+
*/
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
type KthLargest struct {
2+
h *IntHeap
3+
k int
4+
}
5+
6+
func Constructor(k int, nums []int) KthLargest {
7+
h := &IntHeap{}
8+
heap.Init(h)
9+
for _, v := range nums {
10+
heap.Push(h, v)
11+
}
12+
13+
for h.Len() > k {
14+
heap.Pop(h)
15+
}
16+
17+
return KthLargest{
18+
h: h,
19+
k: k,
20+
}
21+
}
22+
23+
func (this *KthLargest) Add(val int) int {
24+
heap.Push(this.h, val)
25+
for this.h.Len() > this.k {
26+
heap.Pop(this.h)
27+
}
28+
29+
return this.h.Top()
30+
}
31+
32+
func connectSticks(sticks []int) int {
33+
h := IntHeap(sticks)
34+
heap.Init(&h)
35+
res := 0
36+
for h.Len() > 1 {
37+
val := heap.Pop(&h).(int)
38+
val += heap.Pop(&h).(int)
39+
res += val
40+
heap.Push(&h, val)
41+
}
42+
return res
43+
}
44+
45+
type IntHeap []int
46+
47+
func (h IntHeap) Len() int { return len(h) }
48+
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
49+
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
50+
func (h *IntHeap) Push(x interface{}) {
51+
*h = append(*h, x.(int))
52+
}
53+
func (h *IntHeap) Pop() interface{} {
54+
old := *h
55+
n := len(old)
56+
x := old[n-1]
57+
*h = old[0 : n-1]
58+
return x
59+
}
60+
61+
func (h *IntHeap) Top() int {
62+
if (*h).Len() == 0 {
63+
return 0
64+
}
65+
66+
return (*h)[0]
67+
}
68+
69+
/**
70+
* Your KthLargest object will be instantiated and called as such:
71+
* obj := Constructor(k, nums);
72+
* param_1 := obj.Add(val);
73+
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class KthLargest {
2+
private PriorityQueue<Integer> q;
3+
private int size;
4+
5+
public KthLargest(int k, int[] nums) {
6+
q = new PriorityQueue<>(k);
7+
size = k;
8+
for (int num : nums) {
9+
add(num);
10+
}
11+
}
12+
13+
public int add(int val) {
14+
q.offer(val);
15+
if (q.size() > size) {
16+
q.poll();
17+
}
18+
return q.peek();
19+
}
20+
}
21+
22+
/**
23+
* Your KthLargest object will be instantiated and called as such:
24+
* KthLargest obj = new KthLargest(k, nums);
25+
* int param_1 = obj.add(val);
26+
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class KthLargest:
2+
3+
def __init__(self, k: int, nums: List[int]):
4+
self.q = []
5+
self.size = k
6+
for num in nums:
7+
self.add(num)
8+
9+
def add(self, val: int) -> int:
10+
heapq.heappush(self.q, val)
11+
if len(self.q) > self.size:
12+
heapq.heappop(self.q)
13+
return self.q[0]
14+
15+
16+
# Your KthLargest object will be instantiated and called as such:
17+
# obj = KthLargest(k, nums)
18+
# param_1 = obj.add(val)

0 commit comments

Comments
 (0)