1
var TopVotedCandidate = function (persons, times) {
2
this.times = times;
3
this.len = times.length;
4
this.votes = new Array(this.len).fill(0);
5

6
let max = 0; // max votes received by any single candidate so far.
7
let leader = -1;
8

9
this.leaders = persons.map((person, i) => {
10
this.votes[person]++;
11

12
if (this.votes[person] >= max) {
13
max = this.votes[person];
14
leader = person;
15
}
16

17
return leader;
18
});
19
};
20

21
TopVotedCandidate.prototype.q = function (t) {
22
let left = 0;
23
let right = this.len - 1;
24

25
while (left <= right) {
26
const mid = left + Math.floor((right - left) / 2);
27

28
if (this.times[mid] === t) return this.leaders[mid];
29
else if (this.times[mid] < t) left = mid + 1;
30
else right = mid - 1;
31
}
32

33
return this.leaders[right];
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0