FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/KthLargestElement.java at master · Orio77/LeetCode · GitHub
Orio77
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode
/
KthLargestElement.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (39 loc) · 1.09 KB
Breadcrumbs
LeetCode
/
KthLargestElement.java
Copy path
File metadata and controls
46 lines (39 loc) · 1.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import
java
.
util
.
Arrays
;
import
java
.
util
.
Comparator
;
import
java
.
util
.
PriorityQueue
;
import
java
.
util
.
stream
.
IntStream
;
class
MyKthLargest
{
private
int
[]
nums
;
private
int
k
;
public
MyKthLargest
(
int
k
,
int
[]
nums
) {
this
.
nums
=
nums
;
this
.
k
=
k
;
}
public
int
add
(
int
val
) {
IntStream
stream
=
Arrays
.
stream
(
nums
);
nums
=
IntStream
.
concat
(
stream
,
IntStream
.
of
(
val
)).
toArray
();
return
Arrays
.
stream
(
nums
).
boxed
().
sorted
(
Comparator
.
reverseOrder
()).
limit
(
k
).
min
(
Integer
::
compare
).
orElse
(
0
);
}
}
class
KthLargest
{
private
PriorityQueue
<
Integer
>
heap
=
new
PriorityQueue
<>();
private
int
k
;
public
KthLargest
(
int
k
,
int
[]
nums
) {
this
.
k
=
k
;
for
(
int
num
:
nums
) {
add
(
num
);
}
}
public
int
add
(
int
val
) {
heap
.
offer
(
val
);
if
(
heap
.
size
() >
k
) {
heap
.
poll
();
}
return
heap
.
peek
();
}
}
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/
Back
|
FazBrowse Home
|
New Git URL