Skip to content

Commit 5e63a97

Browse files
committed
solve problem Kth Largest Element In A Stream
1 parent e0715f8 commit 5e63a97

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ All solutions will be accepted!
220220
|558|[Quad Tree Intersection](https://leetcode-cn.com/problems/quad-tree-intersection/description/)|[java/py](./algorithms/QuadTreeIntersection)|Easy|
221221
|874|[Walking Robot Simulation](https://leetcode-cn.com/problems/walking-robot-simulation/description/)|[java/py/js](./algorithms/WalkingRobotSimulation)|Easy|
222222
|876|[Middle Of The Linked List](https://leetcode-cn.com/problems/middle-of-the-linked-list/description/)|[java/py/js](./algorithms/MiddleOfTheLinkedList)|Easy|
223+
|703|[Kth Largest Element In A Stream](https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/description/KthLargestElementInAStream)|Easy|
223224

224225
# Database
225226
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Kth Largest Element In A Stream
2+
We can solve this problem by heapq in python or PriorityQueue in java
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class KthLargest {
2+
int k;
3+
PriorityQueue<Integer> queue;
4+
5+
public KthLargest(int k, int[] nums) {
6+
this.k = k;
7+
queue = new PriorityQueue<>();
8+
if (nums.length <= k) {
9+
for (int num : nums) queue.add(num);
10+
} else {
11+
for (int i = 0; i < k; i++) queue.add(nums[i]);
12+
for (int i = k; i < nums.length; i++) {
13+
if (queue.peek() < nums[i]) {
14+
queue.poll();
15+
queue.add(nums[i]);
16+
}
17+
}
18+
}
19+
20+
}
21+
22+
public int add(int val) {
23+
if(queue.size() < k) {
24+
queue.offer(val);
25+
} else if(queue.peek() < val) {
26+
queue.poll();
27+
queue.offer(val);
28+
}
29+
return queue.peek();
30+
}
31+
}
32+
33+
/**
34+
* Your KthLargest object will be instantiated and called as such:
35+
* KthLargest obj = new KthLargest(k, nums);
36+
* int param_1 = obj.add(val);
37+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {number} k
3+
* @param {number[]} nums
4+
*/
5+
var KthLargest = function(k, nums) {
6+
let length = nums.length
7+
this.heap = nums.sort((a, b) => a - b)
8+
this.heap = this.heap.slice(length >= k ? length - k : 0)
9+
this.length = k
10+
};
11+
12+
/**
13+
* @param {number} val
14+
* @return {number}
15+
*/
16+
KthLargest.prototype.add = function(val) {
17+
let heapLength = this.heap.length
18+
if (heapLength < this.length || this.heap[0] < val) {
19+
let i = 0,
20+
length = Math.min(this.length, heapLength)
21+
while (i < length) {
22+
if (this.heap[i] >= val) {
23+
break
24+
}
25+
i++
26+
}
27+
this.heap.splice(i, 0, val)
28+
if (heapLength === this.length) this.heap.shift()
29+
}
30+
return this.heap[0]
31+
};
32+
33+
/**
34+
* Your KthLargest object will be instantiated and called as such:
35+
* var obj = Object.create(KthLargest).createNew(k, nums)
36+
* var param_1 = obj.add(val)
37+
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class KthLargest(object):
2+
3+
def __init__(self, k, nums):
4+
"""
5+
:type k: int
6+
:type nums: List[int]
7+
"""
8+
self.pool = nums
9+
self.size = len(self.pool)
10+
self.k = k
11+
heapq.heapify(self.pool)
12+
while self.size > k:
13+
heapq.heappop(self.pool)
14+
self.size -= 1
15+
16+
def add(self, val):
17+
"""
18+
:type val: int
19+
:rtype: int
20+
"""
21+
if self.size < self.k:
22+
heapq.heappush(self.pool, val)
23+
self.size += 1
24+
elif val > self.pool[0]:
25+
heapq.heapreplace(self.pool, val)
26+
return self.pool[0]
27+
28+
29+
# Your KthLargest object will be instantiated and called as such:
30+
# obj = KthLargest(k, nums)
31+
# param_1 = obj.add(val)

0 commit comments

Comments
 (0)