Distribute Candies to People
算了半天等差数列求和和取模,结果发现还不如直接实现。以后这种easy的题,千万别想多
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Solution { public int[] distributeCandies(int candies, int num_people) { int[] res = new int[num_people]; int i = 0; // index int j = 1; // count while(candies > 0) { res[i] += Math.min(j, candies); // find rest of candies candies -= j++; // minus first, then ++ i = ++i%num_people; // ++ first then mod } return res; } } |