Skip to content

Commit 0f3049f

Browse files
Merge pull request algorithm001#595 from Fanlu91/master
week 3 homework id 26
2 parents a46a480 + 6def4b3 commit 0f3049f

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Week_03/id_26/Leetcode_703_26.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fanlu.leetcode.heap;
2+
3+
import java.util.PriorityQueue;
4+
5+
// Source : https://leetcode.com/problems/kth-largest-element-in-a-stream/
6+
// Id : 703
7+
// Author : Fanlu Hai
8+
// Date : 2018-05-05
9+
// Other : should implement priority queue manually
10+
// Tips :
11+
12+
13+
public class KthLargest {
14+
15+
private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
16+
private int size;
17+
18+
public KthLargest(int k, int[] nums) {
19+
this.size = k;
20+
for (int n : nums) {
21+
add(n);
22+
}
23+
}
24+
25+
//99.55% 64.02%
26+
public int add(int val) {
27+
if (minHeap.size() < size)
28+
minHeap.offer(val);
29+
else if (minHeap.peek() < val) {
30+
minHeap.poll();
31+
minHeap.offer(val);
32+
}
33+
return minHeap.peek();
34+
}
35+
36+
}
37+
38+
/**
39+
* Your KthLargest object will be instantiated and called as such:
40+
* KthLargest obj = new KthLargest(k, nums);
41+
* int param_1 = obj.add(val);
42+
*/

Week_03/id_26/Leetcode_997_26.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fanlu.leetcode.graph;
2+
// Source : https://leetcode.com/problems/find-the-town-judge/
3+
// Id : 997
4+
// Author : Fanlu Hai
5+
// Date : 2018-05-03
6+
// Other :
7+
// Tips :
8+
9+
10+
public class FindTheTownJudge {
11+
12+
//98.73% 100.00%
13+
public int findJudge(int N, int[][] trust) {
14+
boolean[] notJudge = new boolean[N + 1];
15+
int[] beingTrusted = new int[N + 1];
16+
17+
for (int[] pair : trust) {
18+
// if trust others, then not judge
19+
notJudge[pair[0]] = true;
20+
21+
boolean not = notJudge[pair[0]];
22+
int beTru = pair[1];
23+
24+
if (notJudge[pair[1]]) {
25+
continue;
26+
}
27+
beingTrusted[pair[1]]++;
28+
}
29+
30+
for (int i = 1; i < N + 1; i++) {
31+
if (beingTrusted[i] == N - 1 && !notJudge[i])
32+
return i;
33+
}
34+
return -1;
35+
}
36+
37+
}

0 commit comments

Comments
 (0)