题目链接
leetcode911
class TopVotedCandidate {
private int[] persons;
private int[] times;
private int length;
int[] dp;
int[] count;
public TopVotedCandidate(int[] persons, int[] times) {
this.persons = persons;
this.times = times;
this.length = times.length;
dp = new int[length];
count = new int[length];
int max = 0;
//求出dp[i],dp数组含义为当前下标为i,选举的人
for (int i = 0; i < length; i++) {
int person = persons[i];
if(++count[person] >= max){
dp[i] = person;
max = count[person];
}else{
dp[i] = dp[i -1];
}
}
System.out.println(Arrays.toString(dp));
}
/**
* 二分查找找到左边界第一个小于等于t的index
*
* @param t
* @return
*/
public int q(int t) {
int left = 0;
int right = length - 1;
while (left < right) {
int mid = (right - left + 1) / 2 + left;
if (times[mid] > t) {
right = mid - 1;
} else {
left = mid;
}
}
return dp[left];
}
}