【LeetCode】 1103. Distribute Candies to People 分糖果 II(Easy)(JAVA)

【LeetCode】 1103. Distribute Candies to People 分糖果 II(Easy)(JAVA)

题目地址: https://leetcode.com/problems/distribute-candies-to-people/

题目描述:

We distribute some number of candies, to a row of n = num_people people in the following way:

We then give 1 candy to the first person, 2 candies to the second person, and so on until we give n candies to the last person.

Then, we go back to the start of the row, giving n + 1 candies to the first person, n + 2 candies to the second person, and so on until we give 2 * n candies to the last person.

This process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies.  The last person will receive all of our remaining candies (not necessarily one more than the previous gift).

Return an array (of length num_people and sum candies) that represents the final distribution of candies.

Example 1:

Input: candies = 7, num_people = 4
Output: [1,2,3,1]
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0,0].
On the third turn, ans[2] += 3, and the array is [1,2,3,0].
On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1].

Example 2:

Input: candies = 10, num_people = 3
Output: [5,2,3]
Explanation: 
On the first turn, ans[0] += 1, and the array is [1,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0].
On the third turn, ans[2] += 3, and the array is [1,2,3].
On the fourth turn, ans[0] += 4, and the final array is [5,2,3].

题目大意

排排坐,分糖果。

我们买了一些糖果 candies,打算把它们分给排好队的 n = num_people 个小朋友。

给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类推,直到给最后一个小朋友 n 颗糖果。

然后,我们再回到队伍的起点,给第一个小朋友 n + 1 颗糖果,第二个小朋友 n + 2 颗,依此类推,直到给最后一个小朋友 2 * n 颗糖果。

重复上述过程(每次都比上一次多给出一颗糖果,当到达队伍终点后再次从队伍起点开始),直到我们分完所有的糖果。注意,就算我们手中的剩下糖果数不够(不比前一次发出的糖果多),这些糖果也会全部发给当前的小朋友。

返回一个长度为 num_people、元素之和为 candies 的数组,以表示糖果的最终分发情况(即 ans[i] 表示第 i 个小朋友分到的糖果数)。

解题方法

主要是找出等差数列求和公式

  1      2      3    ...    n
 1+n    2+n    3+n   ...   n+n
 ...
1+n*k  2+n*k  3+n*k  ...  n+n*k

1、每一行的和 = 上一行和 + n * n;
2、第一次循环,判断下总共有 k 行
3、第 i 个元素的数字为等差数列求和 (i + n * k + i) * (k + 1) / 2;
4、第一个不够的元素加上剩余的,后面的 k - 1,继续等差数列求和

class Solution {
    public int[] distributeCandies(int candies, int num_people) {
        int[] res = new int[num_people];
        int count = 0;
        int sum = (1 + num_people) * num_people / 2;
        while (sum < candies) {
            count++;
            candies -= sum;
            sum += num_people * num_people;
        }
        for (int i = 0; i < num_people; i++) {
            sum = (i + 1 + count * num_people + i + 1) * (count + 1) / 2;
            int cur = i + 1 + count * num_people;
            if (candies == 0) {
                res[i] = sum;
            } else if (cur < candies) {
                res[i] = sum;
                candies -=  count * num_people + (i + 1);
            } else {
                count--;
                res[i] = (i + 1 + count * num_people + i + 1) * (count + 1) / 2 + candies;
                candies = 0;
            }
        }
        return res;
    }
}

执行用时 : 0 ms, 在所有 Java 提交中击败了 100.00% 的用户
内存消耗 : 37 MB, 在所有 Java 提交中击败了 5.27% 的用户

发布了81 篇原创文章 · 获赞 6 · 访问量 2308

猜你喜欢

转载自blog.csdn.net/qq_16927853/article/details/104668444