diff --git a/solution/0900-0999/0911.Online Election/README.md b/solution/0900-0999/0911.Online Election/README.md index cb94da946f29a..c50a22834d1f6 100644 --- a/solution/0900-0999/0911.Online Election/README.md +++ b/solution/0900-0999/0911.Online Election/README.md @@ -47,7 +47,34 @@ ```python - +class TopVotedCandidate: + + def __init__(self, persons: List[int], times: List[int]): + self.persons = persons + self.times = times + mx, cur_win, n = -1, -1, len(persons) + counter = [0] * (n + 1) + self.win_persons = [0] * n + for i, p in enumerate(persons): + counter[p] += 1 + if counter[p] >= mx: + mx = counter[p] + cur_win = p + self.win_persons[i] = cur_win + + def q(self, t: int) -> int: + left, right = 0, len(self.persons) - 1 + while left < right: + mid = (left + right + 1) >> 1 + if self.times[mid] <= t: + left = mid + else: + right = mid - 1 + return self.win_persons[left] + +# Your TopVotedCandidate object will be instantiated and called as such: +# obj = TopVotedCandidate(persons, times) +# param_1 = obj.q(t) ``` ### **Java** @@ -55,7 +82,46 @@ ```java - +class TopVotedCandidate { + private int[] persons; + private int[] times; + private int[] winPersons; + + public TopVotedCandidate(int[] persons, int[] times) { + this.persons = persons; + this.times = times; + int mx = -1, curWin = -1; + int n = persons.length; + int[] counter = new int[n + 1]; + winPersons = new int[n]; + for (int i = 0; i < n; ++i) { + if (++counter[persons[i]] >= mx) { + mx = counter[persons[i]]; + curWin = persons[i]; + } + winPersons[i] = curWin; + } + } + + public int q(int t) { + int left = 0, right = persons.length - 1; + while (left < right) { + int mid = (left + right + 1) >> 1; + if (times[mid] <= t) { + left = mid; + } else { + right = mid - 1; + } + } + return winPersons[left]; + } +} + +/** + * Your TopVotedCandidate object will be instantiated and called as such: + * TopVotedCandidate obj = new TopVotedCandidate(persons, times); + * int param_1 = obj.q(t); + */ ``` ### **...** diff --git a/solution/0900-0999/0911.Online Election/README_EN.md b/solution/0900-0999/0911.Online Election/README_EN.md index fa2819eb36c44..8c5424de08819 100644 --- a/solution/0900-0999/0911.Online Election/README_EN.md +++ b/solution/0900-0999/0911.Online Election/README_EN.md @@ -73,13 +73,79 @@ This continues for 3 more queries at time 15, 24, and 8. ### **Python3** ```python - +class TopVotedCandidate: + + def __init__(self, persons: List[int], times: List[int]): + self.persons = persons + self.times = times + mx, cur_win, n = -1, -1, len(persons) + counter = [0] * (n + 1) + self.win_persons = [0] * n + for i, p in enumerate(persons): + counter[p] += 1 + if counter[p] >= mx: + mx = counter[p] + cur_win = p + self.win_persons[i] = cur_win + + def q(self, t: int) -> int: + left, right = 0, len(self.persons) - 1 + while left < right: + mid = (left + right + 1) >> 1 + if self.times[mid] <= t: + left = mid + else: + right = mid - 1 + return self.win_persons[left] + +# Your TopVotedCandidate object will be instantiated and called as such: +# obj = TopVotedCandidate(persons, times) +# param_1 = obj.q(t) ``` ### **Java** ```java - +class TopVotedCandidate { + private int[] persons; + private int[] times; + private int[] winPersons; + + public TopVotedCandidate(int[] persons, int[] times) { + this.persons = persons; + this.times = times; + int mx = -1, curWin = -1; + int n = persons.length; + int[] counter = new int[n + 1]; + winPersons = new int[n]; + for (int i = 0; i < n; ++i) { + if (++counter[persons[i]] >= mx) { + mx = counter[persons[i]]; + curWin = persons[i]; + } + winPersons[i] = curWin; + } + } + + public int q(int t) { + int left = 0, right = persons.length - 1; + while (left < right) { + int mid = (left + right + 1) >> 1; + if (times[mid] <= t) { + left = mid; + } else { + right = mid - 1; + } + } + return winPersons[left]; + } +} + +/** + * Your TopVotedCandidate object will be instantiated and called as such: + * TopVotedCandidate obj = new TopVotedCandidate(persons, times); + * int param_1 = obj.q(t); + */ ``` ### **...** diff --git a/solution/0900-0999/0911.Online Election/Solution.java b/solution/0900-0999/0911.Online Election/Solution.java new file mode 100644 index 0000000000000..7957c9a2c687a --- /dev/null +++ b/solution/0900-0999/0911.Online Election/Solution.java @@ -0,0 +1,40 @@ +class TopVotedCandidate { + private int[] persons; + private int[] times; + private int[] winPersons; + + public TopVotedCandidate(int[] persons, int[] times) { + this.persons = persons; + this.times = times; + int mx = -1, curWin = -1; + int n = persons.length; + int[] counter = new int[n + 1]; + winPersons = new int[n]; + for (int i = 0; i < n; ++i) { + if (++counter[persons[i]] >= mx) { + mx = counter[persons[i]]; + curWin = persons[i]; + } + winPersons[i] = curWin; + } + } + + public int q(int t) { + int left = 0, right = persons.length - 1; + while (left < right) { + int mid = (left + right + 1) >> 1; + if (times[mid] <= t) { + left = mid; + } else { + right = mid - 1; + } + } + return winPersons[left]; + } +} + +/** + * Your TopVotedCandidate object will be instantiated and called as such: + * TopVotedCandidate obj = new TopVotedCandidate(persons, times); + * int param_1 = obj.q(t); + */ diff --git a/solution/0900-0999/0911.Online Election/Solution.py b/solution/0900-0999/0911.Online Election/Solution.py new file mode 100644 index 0000000000000..8cd3290345fcf --- /dev/null +++ b/solution/0900-0999/0911.Online Election/Solution.py @@ -0,0 +1,28 @@ +class TopVotedCandidate: + + def __init__(self, persons: List[int], times: List[int]): + self.persons = persons + self.times = times + mx, cur_win, n = -1, -1, len(persons) + counter = [0] * (n + 1) + self.win_persons = [0] * n + for i, p in enumerate(persons): + counter[p] += 1 + if counter[p] >= mx: + mx = counter[p] + cur_win = p + self.win_persons[i] = cur_win + + def q(self, t: int) -> int: + left, right = 0, len(self.persons) - 1 + while left < right: + mid = (left + right + 1) >> 1 + if self.times[mid] <= t: + left = mid + else: + right = mid - 1 + return self.win_persons[left] + +# Your TopVotedCandidate object will be instantiated and called as such: +# obj = TopVotedCandidate(persons, times) +# param_1 = obj.q(t)