物业公司网站设计,网站建设总结心得,国外做仿牌网站,应届生出来做网站还是做报纸好在选举中#xff0c;第 i 张票是在时间为 times[i] 时投给 persons[i] 的。
现在#xff0c;我们想要实现下面的查询函数#xff1a; TopVotedCandidate.q(int t) 将返回在 t 时刻主导选举的候选人的编号。
在 t 时刻投出的选票也将被计入我们的查询之中。在平局的情况下第 i 张票是在时间为 times[i] 时投给 persons[i] 的。
现在我们想要实现下面的查询函数 TopVotedCandidate.q(int t) 将返回在 t 时刻主导选举的候选人的编号。
在 t 时刻投出的选票也将被计入我们的查询之中。在平局的情况下最近获得投票的候选人将会获胜。
示例
输入[“TopVotedCandidate”,“q”,“q”,“q”,“q”,“q”,“q”], [[[0,1,1,0,0,1,0],[0,5,10,15,20,25,30]],[3],[12],[25],[15],[24],[8]] 输出[null,0,1,1,0,0,1] 解释 时间为 3票数分布情况是 [0]编号为 0 的候选人领先。 时间为 12票数分布情况是 [0,1,1]编号为 1 的候选人领先。 时间为 25票数分布情况是 [0,1,1,0,0,1]编号为 1 的候选人领先因为最近的投票结果是平局。 在时间 15、24 和 8 处继续执行 3 个查询。
代码 class TopVotedCandidate {int[] helper,time;int n;public TopVotedCandidate(int[] persons, int[] times) {int max0,maxP-1;ntimes.length;MapInteger,Integer mapnew HashMap();helpernew int[n];timetimes;for (int i0;in;i)//计算每个时间节点的赢家{map.put(persons[i],map.getOrDefault(persons[i],0)1);if(map.get(persons[i])max)//更换赢家{maxmap.get(persons[i]);maxPpersons[i];}helper[i]maxP;}}public int q(int t) {int l0,rn-1;while (lr)//二分查找目标时间节点{int mid(r-l)/2l;if(time[mid]t)return helper[mid] ;else if(time[mid]t)lmid1;else rmid-1;}return helper[l-1];}}/*** Your TopVotedCandidate object will be instantiated and called as such:* TopVotedCandidate obj new TopVotedCandidate(persons, times);* int param_1 obj.q(t);*/