March punch card activity on the fifth day LeetCode 1103 title: candy 2 points (simple)
Title: Cruncher, sub-candy. We bought some candy candies, they are going to give queued n = num_people two children. A child to the first confectionery, the second two children, and so on, until the last child to n pieces of candy. Then, we go back to the starting point of the team, to the first pieces of candy children n + 1, n + 2 second child stars, and so on, until the last child to 2 * n pieces of candy. Note that even if the rest is not in our hands the number of candy (candy before issuing more than once), these candies will be distributed to all current children.
Problem-solving ideas: the sugar cycle in turn points.
classSolution{publicint[]distributeCandies(int candies,int num_people){int[] ans =newint[num_people];int i =0;int c =1;while(candies >0){if(candies < c){
ans[i]+=candies;break;}else{
ans[i]+=c;}if(i == num_people-1){
i =0;}else{
i++;}
candies-=c;
c++;}return ans;}}
Problem solution approach: the same idea but the code is more concise solution to a problem, but also a longer running time a little. .
classSolution{publicint[]distributeCandies(int candies,int num_people){int[] ans =newint[num_people];int i =0;while(candies !=0){
ans[i % num_people]+= Math.min(candies, i +1);
candies -= Math.min(candies, i +1);
i +=1;}return ans;}}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/distribute-candies-to-people/solution/fen-tang-guo-ii-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。