File tree 2 files changed +53
-0
lines changed
Contests/Weekly Contest 103
2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ class TopVotedCandidate {
2
+ int [] times ;
3
+ int [] persons ;
4
+ int [] winner ;
5
+
6
+ public TopVotedCandidate (int [] persons , int [] times ) {
7
+ this .times = times ;
8
+ this .persons = persons ;
9
+ int [] counter = new int [persons .length +1 ];
10
+ winner = new int [times .length ];
11
+ int max = Integer .MIN_VALUE ;
12
+ int maxPerson = -1 ;
13
+ int i = 0 ;
14
+
15
+ while (i < times .length ) {
16
+ counter [persons [i ]]++;
17
+ if (max < counter [persons [i ]]) {
18
+ max = counter [persons [i ]];
19
+ maxPerson = persons [i ];
20
+ }
21
+ else if (max == counter [persons [i ]]) {
22
+ maxPerson = persons [i ];
23
+ }
24
+
25
+ winner [i ] = maxPerson ;
26
+
27
+ i ++;
28
+ }
29
+ }
30
+
31
+ public int q (int t ) {
32
+ int i = 0 ;
33
+ while (i < times .length && times [i ] <= t ) {
34
+ i ++;
35
+ }
36
+
37
+ return winner [i -1 ];
38
+ }
39
+ }
40
+
41
+
42
+ /**
43
+ * Your TopVotedCandidate object will be instantiated and called as such:
44
+ * TopVotedCandidate obj = new TopVotedCandidate(persons, times);
45
+ * int param_1 = obj.q(t);
46
+ */
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+
3
+ public static int smallestRangeI (int [] A , int K ) {
4
+ Arrays .sort (A );
5
+ return Math .max ((A [A .length -1 ] - K ) - (A [0 ] + K ), 0 );
6
+ }
7
+ }
You can’t perform that action at this time.
0 commit comments