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