LeetCode #1103 Distribute Candies to People 分糖果 II

1103 Distribute Candies to People 分糖果 II

Description:
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:

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].

Constraints:

1 <= candies <= 10^9
1 <= num_people <= 1000

題目描述:
排排坐,分糖果。

我們買了一些糖果 candies,打算把它們分給排好隊的 n = num_people 個小朋友。

給第一個小朋友 1 顆糖果,第二個小朋友 2 顆,依此類推,直到給最后一個小朋友 n 顆糖果。

然后,我們再回到隊伍的起點,給第一個小朋友 n + 1 顆糖果,第二個小朋友 n + 2 顆,依此類推,直到給最后一個小朋友 2 * n 顆糖果。

重復(fù)上述過程(每次都比上一次多給出一顆糖果,當(dāng)?shù)竭_隊伍終點后再次從隊伍起點開始),直到我們分完所有的糖果。注意,就算我們手中的剩下糖果數(shù)不夠(不比前一次發(fā)出的糖果多),這些糖果也會全部發(fā)給當(dāng)前的小朋友。

返回一個長度為 num_people、元素之和為 candies 的數(shù)組,以表示糖果的最終分發(fā)情況(即 ans[i] 表示第 i 個小朋友分到的糖果數(shù))。

示例 :

示例 1:

輸入:candies = 7, num_people = 4
輸出:[1,2,3,1]
解釋:
第一次,ans[0] += 1,數(shù)組變?yōu)?[1,0,0,0]。
第二次,ans[1] += 2,數(shù)組變?yōu)?[1,2,0,0]。
第三次,ans[2] += 3,數(shù)組變?yōu)?[1,2,3,0]。
第四次,ans[3] += 1(因為此時只剩下 1 顆糖果),最終數(shù)組變?yōu)?[1,2,3,1]。

示例 2:

輸入:candies = 10, num_people = 3
輸出:[5,2,3]
解釋:
第一次,ans[0] += 1,數(shù)組變?yōu)?[1,0,0]。
第二次,ans[1] += 2,數(shù)組變?yōu)?[1,2,0]。
第三次,ans[2] += 3,數(shù)組變?yōu)?[1,2,3]。
第四次,ans[0] += 4,最終數(shù)組變?yōu)?[5,2,3]。

提示:

1 <= candies <= 10^9
1 <= num_people <= 1000

思路:

  1. 按照題目要求分配
    時間復(fù)雜度O(n), 空間復(fù)雜度O(1)
  2. 數(shù)學(xué)法: 除去最后一次分配, 前 n次分配和應(yīng)該是 n * (n + 1) / 2個糖果, 可以解一元二次方程得到 n
    時間復(fù)雜度O(1), 空間復(fù)雜度O(1)

代碼:
C++:

class Solution 
{
public:
    vector<int> distributeCandies(int candies, int num_people) 
    {
        vector<int> result(num_people, 0);
        int count = 0;
        while (candies > 0) 
        {
            result[(count - 1) % num_people] += min(++count, candies);
            candies -= count;
        }
        return result;
    }
};

Java:

class Solution {
    public int[] distributeCandies(int candies, int num_people) {
        int result[] = new int[num_people], count = 0;
        while (candies > 0) {
            result[count % num_people] += Math.min(++count, candies);
            candies -= count;
        }
        return result;
    }
}

Python:

class Solution:
    def distributeCandies(self, candies: int, num_people: int) -> List[int]:
        return [(((2 * x + (int((-1 + (1 + 8 * candies) ** 0.5) / 2) - x) // num_people * num_people) * (1 + (int((-1 + (1 + 8 * candies) ** 0.5) / 2) - x) // num_people)) // 2 + (x == int((-1 + (1 + 8 * candies) ** 0.5) / 2) % num_people + 1) * (candies - (int((-1 + (1 + 8 * candies) ** 0.5) / 2) * (int((-1 + (1 + 8 * candies) ** 0.5) / 2) + 1)) // 2)) for x in range(1, num_people + 1)]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容