Skip to content

Commit 043e29b

Browse files
solves kth largets element in stream
1 parent 5f053ae commit 043e29b

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-170/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-170/2081-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-170/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-171/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-171/2081-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-171/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
88

@@ -191,7 +191,7 @@
191191
| 696 | [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings) | [![Java](assets/java.png)](src/CountBinarySubstrings.java) [![Python](assets/python.png)](python/count_binary_substrings.py) |
192192
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | [![Java](assets/java.png)](src/DegreeOfAnArray.java) [![Python](assets/python.png)](python/degree_of_an_array.py) |
193193
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree) | [![Java](assets/java.png)](src/SearchInBinarySearchTree.java) [![Python](assets/python.png)](python/search_in_binary_search_tree.py) |
194-
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream) | |
194+
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream) | [![Java](assets/java.png)](src/KthLargestElementInAStream.java) [![Python](assets/python.png)](python/k_th_largest_element_in_a_stream.py) |
195195
| 704 | [Binary Search](https://leetcode.com/problems/binary-search) | |
196196
| 705 | [Design HashSet](https://leetcode.com/problems/design-hashset) | |
197197
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap) | |
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import heapq
2+
from typing import List
3+
4+
5+
class KthLargest:
6+
def __init__(self, k: int, nums: List[int]):
7+
self.heap = []
8+
self.heap_size = k
9+
for element in nums:
10+
heapq.heappush(self.heap, element)
11+
while len(self.heap) > self.heap_size:
12+
heapq.heappop(self.heap)
13+
14+
def add(self, val: int) -> int:
15+
heapq.heappush(self.heap, val)
16+
while len(self.heap) > self.heap_size:
17+
heapq.heappop(self.heap)
18+
return self.heap[0]

src/KthLargestElementInAStream.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.PriorityQueue;
2+
import java.util.Queue;
3+
4+
public class KthLargestElementInAStream {
5+
class KthLargest {
6+
7+
private final Queue<Integer> heap = new PriorityQueue<>();
8+
private final int heapSize;
9+
10+
public KthLargest(int k, int[] nums) {
11+
heapSize = k;
12+
for (int element : nums) {
13+
heap.add(element);
14+
}
15+
while (heap.size() > heapSize) {
16+
heap.poll();
17+
}
18+
}
19+
20+
public int add(int val) {
21+
heap.add(val);
22+
while (heap.size() > heapSize) {
23+
heap.poll();
24+
}
25+
return heap.peek();
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)