https://github.com/emahtab/online-election
https://github.com/emahtab/online-election
binary-search leetcode
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/online-election
- Owner: eMahtab
- Created: 2022-05-20T10:42:38.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-20T10:54:04.000Z (over 3 years ago)
- Last Synced: 2025-06-16T04:18:06.342Z (5 months ago)
- Topics: binary-search, leetcode
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Online Election
## https://leetcode.com/problems/online-election
You are given two integer arrays persons and times. In an election, the ith vote was cast for persons[i] at time times[i].
For each query at a time t, find the person that was leading the election at time t. Votes cast at time t will count towards our query. In the case of a tie, the most recent vote (among tied candidates) wins.
Implement the TopVotedCandidate class:
TopVotedCandidate(int[] persons, int[] times) Initializes the object with the persons and times arrays.
int q(int t) Returns the number of the person that was leading the election at time t according to the mentioned rules.
## Implementation :
```java
class Vote {
int person, time;
Vote(int p, int t) {
person = p;
time = t;
}
}
class TopVotedCandidate {
List A;
public TopVotedCandidate(int[] persons, int[] times) {
A = new ArrayList();
Map count = new HashMap();
int leader = -1; // current leader
int m = 0; // current number of votes for leader
for (int i = 0; i < persons.length; ++i) {
int p = persons[i], t = times[i];
int c = count.getOrDefault(p, 0) + 1;
count.put(p, c);
if (c >= m) {
if (p != leader) { // lead change
leader = p;
A.add(new Vote(leader, t));
}
if (c > m) m = c;
}
}
}
public int q(int t) {
int lo = 1, hi = A.size();
while (lo < hi) {
int mi = lo + (hi - lo) / 2;
if (A.get(mi).time <= t)
lo = mi + 1;
else
hi = mi;
}
return A.get(lo - 1).person;
}
}
```
## References :
https://leetcode.com/problems/online-election/solution