class TopVotedCandidate { TreeMap<Integer,Integer> tree = new TreeMap<>(); public TopVotedCandidate(int[] persons, int[] times) { int[][] input = new int[persons.length][2]; for (int i = 0; i < persons.length; i++) { input[i][0] = times[i]; input[i][1] = persons[i]; } Map<Integer, Integer> map = new HashMap<>(); // res[0] 保存最大值 // res[1] 保存对应人 int maxVotedCount = 0; int maxVotedPersonIndex = 0; for (int i = 0; i < persons.length; i++) { int k = map.containsKey(input[i][1]) ? (map.get(input[i][1]) + 1) : 1; map.put(input[i][1], k); if (maxVotedCount <= k) { maxVotedCount = k; maxVotedPersonIndex = input[i][1]; } tree.put(input[i][0], maxVotedPersonIndex); } } public int q(int t) { return tree.floorEntry(t).getValue(); }}