Last active
May 28, 2019 23:00
-
-
Save ogregoire/730e1cbbc45b31a180716050c57824e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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