Skip to content

Commit 2287a5f

Browse files
committed
Added 2 solutions
1 parent 5cebfb7 commit 2287a5f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class KthLargest {
2+
PriorityQueue<Integer> priorityQueue;
3+
int k;
4+
5+
public KthLargest(int k, int[] nums) {
6+
this.k = k;
7+
priorityQueue = new PriorityQueue<>(k);
8+
9+
for (int num : nums) {
10+
priorityQueue.offer(num);
11+
if (priorityQueue.size() > k) {
12+
priorityQueue.poll();
13+
}
14+
}
15+
}
16+
17+
public int add(int val) {
18+
priorityQueue.offer(val);
19+
if (priorityQueue.size() > k) {
20+
priorityQueue.poll();
21+
}
22+
23+
return priorityQueue.peek();
24+
}
25+
}
26+
27+
/**
28+
* Your KthLargest object will be instantiated and called as such:
29+
* KthLargest obj = new KthLargest(k, nums);
30+
* int param_1 = obj.add(val);
31+
*/

Medium/H-Index.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int hIndex(int[] citations) {
3+
Arrays.sort(citations);
4+
int end = citations.length;
5+
int ind = 0;
6+
7+
for (int i=0; i<end; i++) {
8+
if (citations[i] >= end - i) {
9+
ind = end - i;
10+
break;
11+
}
12+
}
13+
14+
return ind;
15+
}
16+
}

0 commit comments

Comments
 (0)