An open API service indexing awesome lists of open source software.

https://github.com/emahtab/online-election


https://github.com/emahtab/online-election

binary-search leetcode

Last synced: 3 months ago
JSON representation

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