Skip to content

Instantly share code, notes, and snippets.

@ogregoire
Last active May 28, 2019 23:00
Show Gist options
  • Save ogregoire/730e1cbbc45b31a180716050c57824e1 to your computer and use it in GitHub Desktop.
Save ogregoire/730e1cbbc45b31a180716050c57824e1 to your computer and use it in GitHub Desktop.
public class DhondtMethod {
public int[] partition(int totalSeats, int[] votes) {
int[] seats = new int[votes.length];
if (totalSeats == 0) {
return seats;
}
if (votes.length == 0) {
return seats;
}
for (int seat = 0; seat < totalSeats; seat++) {
int bestSubject = 0;
for (int subject = 0, max = Integer.MIN_VALUE; subject < votes.length; subject++) {
int candidate = Math.round(votes[subject] / (seats[subject] + 1.0f));
if (candidate > max) {
bestSubject = subject;
max = candidate;
}
}
seats[bestSubject]++;
}
return seats;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment