Skip to content

Commit 56b9a57

Browse files
committed
Added code for problems solved in Weekly contest 103
1 parent 219c0de commit 56b9a57

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
*/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

0 commit comments

Comments
 (0)