class TopVotedCandidate {
public:
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
}
int q(int t) {
}
};
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
* int param_1 = obj->q(t);
*/
class TopVotedCandidate:
def __init__(self, persons: List[int], times: List[int]):
tops = []
voteCounts = defaultdict(int)
voteCounts[-1] = -1
top = -1
for p in persons:
voteCounts[p] += 1
if voteCounts[p] >= voteCounts[top]:
top = p
tops.append(top)
self.tops = tops
self.times = times
def q(self, t: int) -> int:
l, r = 0, len(self.times) - 1
# 找到满足 times[l] <= t 的最大的 l
while l < r:
m = l + (r - l + 1) // 2
if self.times[m] <= t:
l = m
else:
r = m - 1
# 也可以使用内置的二分查找的包来确定 l
# l = bisect.bisect(self.times, t) - 1
return self.tops[l]
# Your TopVotedCandidate object will be instantiated and called as such:
# obj = TopVotedCandidate(persons, times)
# param_1 = obj.q(t)
class TopVotedCandidate {
List<Integer> tops;
Map<Integer, Integer> voteCounts;
int[] times;
public TopVotedCandidate(int[] persons, int[] times) {
tops = new ArrayList<Integer>();
voteCounts = new HashMap<Integer, Integer>();
voteCounts.put(-1, -1);
int top = -1;
for (int i = 0; i < persons.length; ++i) {
int p = persons[i];
voteCounts.put(p, voteCounts.getOrDefault(p, 0) + 1);
if (voteCounts.get(p) >= voteCounts.get(top)) {
top = p;
}
tops.add(top);
}
this.times = times;
}
public int q(int t) {
int l = 0, r = times.length - 1;
// 找到满足 times[l] <= t 的最大的 l
while (l < r) {
int m = l + (r - l + 1) / 2;
if (times[m] <= t) {
l = m;
} else {
r = m - 1;
}
}
return tops.get(l);
}
}
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate obj = new TopVotedCandidate(persons, times);
* int param_1 = obj.q(t);
*/
type TopVotedCandidate struct {
tops, times []int
}
func Constructor(persons, times []int) TopVotedCandidate {
tops := []int{}
top := -1
voteCounts := map[int]int{-1: -1}
for _, p := range persons {
voteCounts[p]++
if voteCounts[p] >= voteCounts[top] {
top = p
}
tops = append(tops, top)
}
return TopVotedCandidate{tops, times}
}
func (c *TopVotedCandidate) Q(t int) int {
return c.tops[sort.SearchInts(c.times, t+1)-1]
}
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* obj := Constructor(persons, times);
* param_1 := obj.Q(t);
*/
/**
* @param {number[]} persons
* @param {number[]} times
*/
var TopVotedCandidate = function(persons, times) {
this.tops = [];
this.voteCounts = new Map();
this.voteCounts.set(-1, -1);
this.times = times;
let top = -1;
for (let i = 0; i < persons.length; ++i) {
const p = persons[i];
if (!this.voteCounts.has(p)) {
this.voteCounts.set(p, 0);
} else {
this.voteCounts.set(p, this.voteCounts.get(p) + 1);
}
if (this.voteCounts.get(p) >= this.voteCounts.get(top)) {
top = p;
}
this.tops.push(top);
}
};
/**
* @param {number} t
* @return {number}
*/
TopVotedCandidate.prototype.q = function(t) {
let l = 0, r = this.times.length - 1;
// 找到满足 times[l] <= t 的最大的 l
while (l < r) {
const m = l + Math.floor((r - l + 1) / 2);
if (this.times[m] <= t) {
l = m;
} else {
r = m - 1;
}
}
return this.tops[l];
};
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* var obj = new TopVotedCandidate(persons, times)
* var param_1 = obj.q(t)
*/