-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.ts
44 lines (41 loc) · 1.16 KB
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class TopVotedCandidate {
private times: number[];
private wins: number[];
constructor(persons: number[], times: number[]) {
const n = persons.length;
this.times = times;
this.wins = new Array<number>(n).fill(0);
const cnt: Array<number> = new Array<number>(n).fill(0);
let cur = 0;
for (let i = 0; i < n; ++i) {
const p = persons[i];
cnt[p]++;
if (cnt[cur] <= cnt[p]) {
cur = p;
}
this.wins[i] = cur;
}
}
q(t: number): number {
const search = (t: number): number => {
let l = 0,
r = this.times.length;
while (l < r) {
const mid = (l + r) >> 1;
if (this.times[mid] > t) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
const i = search(t) - 1;
return this.wins[i];
}
}
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* var obj = new TopVotedCandidate(persons, times)
* var param_1 = obj.q(t)
*/