File tree 2 files changed +47
-0
lines changed
2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change
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
+ */
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments