Skip to content

Instantly share code, notes, and snippets.

@lishunan246
Created August 17, 2020 15:56
Show Gist options
  • Save lishunan246/54d0739944ffdb417c2bf5316107fec6 to your computer and use it in GitHub Desktop.
Save lishunan246/54d0739944ffdb417c2bf5316107fec6 to your computer and use it in GitHub Desktop.
Distribute Candies to People
class Solution {
public:
vector<int> distributeCandies(int candies, int num_people) {
vector<int> v(num_people, 0);
int round = 0;
int s = (1 + num_people) * num_people / 2;
int s_all = s;
while (candies > s_all) {
round++;
s += num_people * num_people;
s_all += s;
}
if (round > 0) {
for (int i = 0; i < v.size(); ++i) {
v[i] = (i + 1 + (i + 1 + (round - 1) * num_people)) * round / 2;
candies -= v[i];
}
}
for (int i = 0; i < v.size(); ++i) {
int cur = i + 1 + round * num_people;
if (cur <= candies) {
v[i] += cur;
candies -= cur;
}
else {
v[i] += candies;
break;
}
}
return v;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment