Last active
August 18, 2020 19:44
-
-
Save IngeFrodo/c5fcc636e722b2abf071cd2a65cd62cc 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
class DistributeCandiesToPeople { | |
public int[] distributeCandies(int candies, int num_people) { | |
int n = num_people; | |
int turns = 0; | |
int sum = n * (n + 1) / 2; | |
int n2 = n * n; | |
while (sum <= candies) { | |
candies -= sum; | |
sum += n2; | |
turns++; | |
} | |
int columns = (turns - 1) * turns / 2; | |
int[] people = new int[n]; | |
for (int i = 0; i < n; i++) { | |
people[i] = columns * n + (i + 1) * turns; | |
} | |
int candie = Math.min(candies, turns * n + 1); | |
for (int i = 0; candies > 0; i++) { | |
people[i] += candie; | |
candies -= candie; | |
candie = Math.min(candies, candie + 1); | |
} | |
return people; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment