package leetcode;

/**
 * https://leetcode.com/problems/online-election/
 */
public class Problem911 {
    private static class TopVotedCandidate {
        public TopVotedCandidate(int[] persons, int[] times) {
        }

        public int q(int t) {
            // TODO
            return 0;
        }
    }

    public static void main(String[] args) {
        TopVotedCandidate candidate = new TopVotedCandidate(new int[]{0, 1, 1, 0, 0, 1, 0},
            new int[]{0, 5, 10, 15, 20, 25, 30});
        System.out.println(candidate.q(3)); // 0
        System.out.println(candidate.q(12)); // 1
        System.out.println(candidate.q(25)); // 1
        System.out.println(candidate.q(15)); // 0
        System.out.println(candidate.q(24)); // 0
        System.out.println(candidate.q(8)); // 1
    }
}

@@ -12,16 +12,91 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.TreeSet;%0A%0A
 /**%0A * h
@@ -229,143 +229,1109 @@
    p
-ublic TopVotedCandidate(int%5B%5D persons, int%5B%5D times) %7B%0A        %7D%0A%0A        public int q(int t) %7B%0A            // TODO%0A            return 0
+rivate final Map%3CInteger /*time*/, Integer /*person*/%3E timePerson = new HashMap%3C%3E();%0A        private final TreeSet%3CInteger%3E timeSet = new TreeSet%3C%3E();%0A%0A        public TopVotedCandidate(int%5B%5D persons, int%5B%5D times) %7B%0A            int maxPerson = 0;%0A            Map%3CInteger /*person*/, Integer /*count*/%3E personCount = new HashMap%3C%3E();%0A            for (int i = 0; i %3C times.length; i++) %7B%0A                timeSet.add(times%5Bi%5D);%0A                int count = 0;%0A                if (!personCount.containsKey(persons%5Bi%5D)) %7B%0A                    count = 1;%0A                %7D else %7B%0A                    count = personCount.get(persons%5Bi%5D) + 1;%0A                %7D%0A                personCount.put(persons%5Bi%5D, count);%0A                if (personCount.get(maxPerson) %3C= count) %7B%0A                    maxPerson = persons%5Bi%5D;%0A                %7D%0A                timePerson.put(times%5Bi%5D, maxPerson);%0A            %7D%0A        %7D%0A%0A        public int q(int t) %7B%0A            Integer time = timeSet.floor(t);%0A            if (time == null) %7B%0A                time = timeSet.first();%0A            %7D%0A            return timePerson.get(time)
 ;%0A  
@@ -1532,16 +1532,65 @@
 , 30%7D);%0A
+        System.out.println(candidate.q(0)); // 0%0A
         
@@ -1875,20 +1875,120 @@
 q(8)); // 1%0A
+        System.out.println(candidate.q(30)); // 0%0A        System.out.println(candidate.q(35)); // 0%0A
     %7D%0A%7D%0A

Update problem 911