From a7bfa661c6ae94e7774baad35c430ba55510c362 Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Thu, 4 Nov 2021 13:49:51 +0800 Subject: [PATCH 1/5] feat: add solutions to lc problem: No.0911.Online Election --- .../0900-0999/0911.Online Election/README.md | 65 ++++++++++++++++++- .../0911.Online Election/README_EN.md | 65 ++++++++++++++++++- .../0911.Online Election/Solution.java | 35 ++++++++++ .../0911.Online Election/Solution.py | 28 ++++++++ 4 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 solution/0900-0999/0911.Online Election/Solution.java create mode 100644 solution/0900-0999/0911.Online Election/Solution.py diff --git a/solution/0900-0999/0911.Online Election/README.md b/solution/0900-0999/0911.Online Election/README.md index cb94da946f29a..55a6a7b092709 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.times = times + self.persons = persons + n = len(persons) + win_person = [0] * n + count = [0] * n + cur_max = -1 + cur_win = -1 + for i in range(n): + count[persons[i]] += 1 + if count[persons[i]] >= cur_max: + cur_win = persons[i] + cur_max = count[persons[i]] + win_person[i] = cur_win + self.win_person = win_person + + def q(self, t: int) -> int: + times = self.times + win_person = self.win_person + left, right = 0, len(times) - 1 + while left < right: + mid = (left + right + 1) // 2 + if times[mid] <= t: + left = mid + else: + right = mid - 1 + return win_person[left] ``` ### **Java** @@ -55,7 +82,41 @@ ```java - +class TopVotedCandidate { + int[] persons; + int[] times; + int[] winPerson; + + public TopVotedCandidate(int[] persons, int[] times) { + this.persons = persons; + this.times = times; + int len = times.length; + winPerson = new int[len]; + int max = 0; + int curWin = -1; + int[] count = new int[len]; + for (int i = 0; i < len; i++) { + if (++count[persons[i]] >= max) { + max = count[persons[i]]; + curWin = persons[i]; + } + winPerson[i] = curWin; + } + } + + public int q(int t) { + int left = 0, right = times.length - 1; + while (left < right) { + int mid = (left + right + 1) >> 1; + if (times[mid] <= t) { + left = mid; + } else { + right = mid - 1; + } + } + return winPerson[left]; + } +} ``` ### **...** diff --git a/solution/0900-0999/0911.Online Election/README_EN.md b/solution/0900-0999/0911.Online Election/README_EN.md index fa2819eb36c44..02ffe0aeca25c 100644 --- a/solution/0900-0999/0911.Online Election/README_EN.md +++ b/solution/0900-0999/0911.Online Election/README_EN.md @@ -73,13 +73,74 @@ 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.times = times + self.persons = persons + n = len(persons) + win_person = [0] * n + count = [0] * n + cur_max = -1 + cur_win = -1 + for i in range(n): + count[persons[i]] += 1 + if count[persons[i]] >= cur_max: + cur_win = persons[i] + cur_max = count[persons[i]] + win_person[i] = cur_win + self.win_person = win_person + + def q(self, t: int) -> int: + times = self.times + win_person = self.win_person + left, right = 0, len(times) - 1 + while left < right: + mid = (left + right + 1) // 2 + if times[mid] <= t: + left = mid + else: + right = mid - 1 + return win_person[left] ``` ### **Java** ```java - +class TopVotedCandidate { + int[] persons; + int[] times; + int[] winPerson; + + public TopVotedCandidate(int[] persons, int[] times) { + this.persons = persons; + this.times = times; + int len = times.length; + winPerson = new int[len]; + int max = 0; + int curWin = -1; + int[] count = new int[len]; + for (int i = 0; i < len; i++) { + if (++count[persons[i]] >= max) { + max = count[persons[i]]; + curWin = persons[i]; + } + winPerson[i] = curWin; + } + } + + public int q(int t) { + int left = 0, right = times.length - 1; + while (left < right) { + int mid = (left + right + 1) >> 1; + if (times[mid] <= t) { + left = mid; + } else { + right = mid - 1; + } + } + return winPerson[left]; + } +} ``` ### **...** 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..9060a19754777 --- /dev/null +++ b/solution/0900-0999/0911.Online Election/Solution.java @@ -0,0 +1,35 @@ +class TopVotedCandidate { + int[] persons; + int[] times; + int[] winPerson; + + public TopVotedCandidate(int[] persons, int[] times) { + this.persons = persons; + this.times = times; + int len = times.length; + winPerson = new int[len]; + int max = 0; + int curWin = -1; + int[] count = new int[len]; + for (int i = 0; i < len; i++) { + if (++count[persons[i]] >= max) { + max = count[persons[i]]; + curWin = persons[i]; + } + winPerson[i] = curWin; + } + } + + public int q(int t) { + int left = 0, right = times.length - 1; + while (left < right) { + int mid = (left + right + 1) >> 1; + if (times[mid] <= t) { + left = mid; + } else { + right = mid - 1; + } + } + return winPerson[left]; + } +} \ No newline at end of file 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..63480b617cd24 --- /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.times = times + self.persons = persons + n = len(persons) + win_person = [0] * n + count = [0] * n + cur_max = -1 + cur_win = -1 + for i in range(n): + count[persons[i]] += 1 + if count[persons[i]] >= cur_max: + cur_win = persons[i] + cur_max = count[persons[i]] + win_person[i] = cur_win + self.win_person = win_person + + def q(self, t: int) -> int: + times = self.times + win_person = self.win_person + left, right = 0, len(times) - 1 + while left < right: + mid = (left + right + 1) // 2 + if times[mid] <= t: + left = mid + else: + right = mid - 1 + return win_person[left] \ No newline at end of file From 819018b3024b628ae9bf7c3a412267bdf3825bd3 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 5 Nov 2021 10:09:17 +0800 Subject: [PATCH 2/5] Update README.md --- .../0900-0999/0911.Online Election/README.md | 73 ++++++++++--------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/solution/0900-0999/0911.Online Election/README.md b/solution/0900-0999/0911.Online Election/README.md index 55a6a7b092709..c50a22834d1f6 100644 --- a/solution/0900-0999/0911.Online Election/README.md +++ b/solution/0900-0999/0911.Online Election/README.md @@ -48,33 +48,33 @@ ```python class TopVotedCandidate: + def __init__(self, persons: List[int], times: List[int]): - self.times = times self.persons = persons - n = len(persons) - win_person = [0] * n - count = [0] * n - cur_max = -1 - cur_win = -1 - for i in range(n): - count[persons[i]] += 1 - if count[persons[i]] >= cur_max: - cur_win = persons[i] - cur_max = count[persons[i]] - win_person[i] = cur_win - self.win_person = win_person + 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: - times = self.times - win_person = self.win_person - left, right = 0, len(times) - 1 + left, right = 0, len(self.persons) - 1 while left < right: - mid = (left + right + 1) // 2 - if times[mid] <= t: + mid = (left + right + 1) >> 1 + if self.times[mid] <= t: left = mid else: right = mid - 1 - return win_person[left] + 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** @@ -83,29 +83,28 @@ class TopVotedCandidate: ```java class TopVotedCandidate { - int[] persons; - int[] times; - int[] winPerson; + private int[] persons; + private int[] times; + private int[] winPersons; public TopVotedCandidate(int[] persons, int[] times) { this.persons = persons; this.times = times; - int len = times.length; - winPerson = new int[len]; - int max = 0; - int curWin = -1; - int[] count = new int[len]; - for (int i = 0; i < len; i++) { - if (++count[persons[i]] >= max) { - max = count[persons[i]]; + 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]; } - winPerson[i] = curWin; + winPersons[i] = curWin; } } - + public int q(int t) { - int left = 0, right = times.length - 1; + int left = 0, right = persons.length - 1; while (left < right) { int mid = (left + right + 1) >> 1; if (times[mid] <= t) { @@ -114,9 +113,15 @@ class TopVotedCandidate { right = mid - 1; } } - return winPerson[left]; + 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); + */ ``` ### **...** From 7273467d95ff0acfb0c0b2cdb9df0bdc9b5f0b04 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 5 Nov 2021 10:09:57 +0800 Subject: [PATCH 3/5] Update README_EN.md --- .../0911.Online Election/README_EN.md | 73 ++++++++++--------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/solution/0900-0999/0911.Online Election/README_EN.md b/solution/0900-0999/0911.Online Election/README_EN.md index 02ffe0aeca25c..8c5424de08819 100644 --- a/solution/0900-0999/0911.Online Election/README_EN.md +++ b/solution/0900-0999/0911.Online Election/README_EN.md @@ -74,62 +74,61 @@ This continues for 3 more queries at time 15, 24, and 8. ```python class TopVotedCandidate: + def __init__(self, persons: List[int], times: List[int]): - self.times = times self.persons = persons - n = len(persons) - win_person = [0] * n - count = [0] * n - cur_max = -1 - cur_win = -1 - for i in range(n): - count[persons[i]] += 1 - if count[persons[i]] >= cur_max: - cur_win = persons[i] - cur_max = count[persons[i]] - win_person[i] = cur_win - self.win_person = win_person + 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: - times = self.times - win_person = self.win_person - left, right = 0, len(times) - 1 + left, right = 0, len(self.persons) - 1 while left < right: - mid = (left + right + 1) // 2 - if times[mid] <= t: + mid = (left + right + 1) >> 1 + if self.times[mid] <= t: left = mid else: right = mid - 1 - return win_person[left] + 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 { - int[] persons; - int[] times; - int[] winPerson; + private int[] persons; + private int[] times; + private int[] winPersons; public TopVotedCandidate(int[] persons, int[] times) { this.persons = persons; this.times = times; - int len = times.length; - winPerson = new int[len]; - int max = 0; - int curWin = -1; - int[] count = new int[len]; - for (int i = 0; i < len; i++) { - if (++count[persons[i]] >= max) { - max = count[persons[i]]; + 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]; } - winPerson[i] = curWin; + winPersons[i] = curWin; } } - + public int q(int t) { - int left = 0, right = times.length - 1; + int left = 0, right = persons.length - 1; while (left < right) { int mid = (left + right + 1) >> 1; if (times[mid] <= t) { @@ -138,9 +137,15 @@ class TopVotedCandidate { right = mid - 1; } } - return winPerson[left]; + 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); + */ ``` ### **...** From 5028f57a2c418bcb54ef0623620f600b97d21768 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 5 Nov 2021 10:10:17 +0800 Subject: [PATCH 4/5] Update Solution.java --- .../0911.Online Election/Solution.java | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/solution/0900-0999/0911.Online Election/Solution.java b/solution/0900-0999/0911.Online Election/Solution.java index 9060a19754777..7957c9a2c687a 100644 --- a/solution/0900-0999/0911.Online Election/Solution.java +++ b/solution/0900-0999/0911.Online Election/Solution.java @@ -1,27 +1,26 @@ class TopVotedCandidate { - int[] persons; - int[] times; - int[] winPerson; + private int[] persons; + private int[] times; + private int[] winPersons; public TopVotedCandidate(int[] persons, int[] times) { this.persons = persons; this.times = times; - int len = times.length; - winPerson = new int[len]; - int max = 0; - int curWin = -1; - int[] count = new int[len]; - for (int i = 0; i < len; i++) { - if (++count[persons[i]] >= max) { - max = count[persons[i]]; + 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]; } - winPerson[i] = curWin; + winPersons[i] = curWin; } } - + public int q(int t) { - int left = 0, right = times.length - 1; + int left = 0, right = persons.length - 1; while (left < right) { int mid = (left + right + 1) >> 1; if (times[mid] <= t) { @@ -30,6 +29,12 @@ public int q(int t) { right = mid - 1; } } - return winPerson[left]; + return winPersons[left]; } -} \ No newline at end of file +} + +/** + * Your TopVotedCandidate object will be instantiated and called as such: + * TopVotedCandidate obj = new TopVotedCandidate(persons, times); + * int param_1 = obj.q(t); + */ From 5d7f87d91535202cb0274bb1d6edb9cd3e4eec5b Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 5 Nov 2021 10:10:34 +0800 Subject: [PATCH 5/5] Update Solution.py --- .../0911.Online Election/Solution.py | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/solution/0900-0999/0911.Online Election/Solution.py b/solution/0900-0999/0911.Online Election/Solution.py index 63480b617cd24..8cd3290345fcf 100644 --- a/solution/0900-0999/0911.Online Election/Solution.py +++ b/solution/0900-0999/0911.Online Election/Solution.py @@ -1,28 +1,28 @@ class TopVotedCandidate: + def __init__(self, persons: List[int], times: List[int]): - self.times = times self.persons = persons - n = len(persons) - win_person = [0] * n - count = [0] * n - cur_max = -1 - cur_win = -1 - for i in range(n): - count[persons[i]] += 1 - if count[persons[i]] >= cur_max: - cur_win = persons[i] - cur_max = count[persons[i]] - win_person[i] = cur_win - self.win_person = win_person + 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: - times = self.times - win_person = self.win_person - left, right = 0, len(times) - 1 + left, right = 0, len(self.persons) - 1 while left < right: - mid = (left + right + 1) // 2 - if times[mid] <= t: + mid = (left + right + 1) >> 1 + if self.times[mid] <= t: left = mid else: right = mid - 1 - return win_person[left] \ No newline at end of file + return self.win_persons[left] + +# Your TopVotedCandidate object will be instantiated and called as such: +# obj = TopVotedCandidate(persons, times) +# param_1 = obj.q(t)