zoukankan      html  css  js  c++  java
  • [LeetCode] 1103. 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].

    Constraints:

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

    分糖果。

    题意是你有一些糖果,需要分给一组人。分发的顺序是从第一个人到最后一个人,每个元素比他之前的那个元素多发一个。还有剩余糖果的话,再回到第一个人,比最后一个人再多发一个。如果剩下糖果不满足这个规则,则把剩余的糖果全部分给当前这个人。

    这是一道数学题。我这里给出暴力解,就是按照题目的思路给每个人分发糖果。需要创建一个变量记录遍历的轮数,其他环节都比较直观了,参见代码。

    时间O(max(根号糖果数,人数))

    空间O(n)

    Java实现

     1 class Solution {
     2     public int[] distributeCandies(int candies, int num_people) {
     3         int round = 0;
     4         int[] res = new int[num_people];
     5         while (candies > 0) {
     6             for (int i = 1; i <= res.length; i++) {
     7                 res[i - 1] += Math.min(candies, i + num_people * round);
     8                 candies -= num_people * round + i;
     9                 if (candies <= 0) {
    10                     break;
    11                 }
    12             }
    13             round++;
    14         }
    15         return res;
    16     }
    17 }

    JavaScript实现

     1 /**
     2  * @param {number} candies
     3  * @param {number} num_people
     4  * @return {number[]}
     5  */
     6 var distributeCandies = function (candies, num_people) {
     7     let res = Array(num_people).fill(0);
     8     let n = 0;
     9     while (candies > 0) {
    10         for (let i = 1; i <= num_people; i++) {
    11             res[i - 1] += Math.min(candies, i + n * num_people);
    12             candies -= i + n * num_people;
    13             if (candies <= 0) {
    14                 break;
    15             }
    16         }
    17         n++;
    18     }
    19     return res;
    20 };

    LeetCode 题目总结

  • 相关阅读:
    [BZOJ 3774] 最优选择 【最小割】
    [XJOI NOI2015模拟题13] C 白黑树 【线段树合并】
    [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
    [XJOI NOI2015模拟题13] B 最小公倍数 【找规律】
    [XJOI NOI2015模拟题13] A 神奇的矩阵 【分块】
    [XJOI NOI02015训练题7] B 线线线 【二分】
    Java+Maven+selenium+testng+reportng自动化测试框架
    selenium实例:unittest框架+PO开发模式
    WebDriver驱动下载地址
    testng 执行多个suite
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13375413.html
Copyright © 2011-2022 走看看