Skip to content

Commit c43659d

Browse files
Merge pull request algorithm001#570 from oxcz/master
135
2 parents 2204deb + 8ba70b3 commit c43659d

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class KthLargest {
2+
3+
final PriorityQueue<Integer> q ;
4+
final int k;
5+
public KthLargest(int k, int[] nums) {
6+
this.k = k;
7+
q = new PriorityQueue<Integer>(k);
8+
for(int i: nums) {
9+
add(i);
10+
}
11+
}
12+
13+
public int add(int val) {
14+
if(q.size() < k) {
15+
q.offer(val);
16+
17+
}
18+
else if(q.peek() < val) {
19+
q.poll();
20+
q.offer(val);
21+
}
22+
return q.peek();
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int findJudge(int N, int[][] trust) {
3+
4+
if(N == 1){
5+
return N;
6+
}
7+
HashMap<Integer ,Integer> hashMap = new HashMap(trust.length);
8+
int[] ints = new int[trust.length];
9+
10+
for(int i=0;i<trust.length;i++){
11+
ints[i] = trust[i][0];
12+
hashMap.put(trust[i][1],hashMap.getOrDefault(trust[i][1],0)+1);
13+
}
14+
15+
for(Integer key :hashMap.keySet()){
16+
if(hashMap.get(key) ==N-1 ){
17+
for(Integer s:ints){
18+
if(s.equals(key)){
19+
return -1;
20+
}
21+
}
22+
return key;
23+
}
24+
}
25+
return -1;
26+
}
27+
}

0 commit comments

Comments
 (0)