1. class TopVotedCandidate {
    2. TreeMap<Integer,Integer> tree = new TreeMap<>();
    3. public TopVotedCandidate(int[] persons, int[] times) {
    4. int[][] input = new int[persons.length][2];
    5. for (int i = 0; i < persons.length; i++) {
    6. input[i][0] = times[i];
    7. input[i][1] = persons[i];
    8. }
    9. Map<Integer, Integer> map = new HashMap<>();
    10. // res[0] 保存最大值
    11. // res[1] 保存对应人
    12. int maxVotedCount = 0;
    13. int maxVotedPersonIndex = 0;
    14. for (int i = 0; i < persons.length; i++) {
    15. int k = map.containsKey(input[i][1]) ? (map.get(input[i][1]) + 1) : 1;
    16. map.put(input[i][1], k);
    17. if (maxVotedCount <= k) {
    18. maxVotedCount = k;
    19. maxVotedPersonIndex = input[i][1];
    20. }
    21. tree.put(input[i][0], maxVotedPersonIndex);
    22. }
    23. }
    24. public int q(int t) {
    25. return tree.floorEntry(t).getValue();
    26. }
    27. }