Preface explains

Algorithm learning, daily brush problem record.

Subject to connect

The points of candy II

The subject content

Sit in rows and share the candy.

We bought a few candies, and we were going to distribute them to the children who were standing in line.

Give the first child 1 candy, the second child 2 candy, and so on until the last child has n candy.

Then we went back to the beginning of the line and gave n + 1 candy to the first child, n + 2 candy to the second child, and so on until we gave 2 * N candy to the last child.

Repeat the process (giving one more candy each time, and starting again at the beginning of the team when we reach the end of the team) until we have divided all the candy. Note that even if we don’t have enough candy left (no more than we did before), all the candy will be given to the current child.

Return an array of length NUM_people and sum of elements to represent the final distribution of candies (ans[I] is the number of candies to the i-th child).

Example 1:

Enter: Candies = 7, num_people = 4

Output:,2,3,1 [1]

Explanation:

The first time, ans[0] += 1, the array changes to [1,0,0,0].

The second time, ans[1] += 2, the array becomes [1,2,0,0].

3, ans[2] += 3, array [1,2,3,0]

The fourth time, ans[3] += 1, because there is only one candy left, the array becomes [1,2,3,1].

Example 2:

Enter: Candies = 10, num_people = 3

Output: [5, 2, 3]

Explanation:

The first time, ans[0] += 1, the array changes to [1,0,0].

The second time, ans[1] += 2, the array becomes [1,2,0].

(ans[2] += 3) (ans[2] += 3)

The fourth time, ans[0] += 4, the final array becomes [5,2,3].

Tip:

1 <= candies <= 10^9

1 <= num_people <= 1000

The analysis process

Loop to see if the number of candies is greater than zero.

Define n candies to be divided each time, and n increases by 1 on each cycle.

Define the index of the result array as index. Each time we loop, if we are not at the last position, the index incrementing by one, and if we are at the last position, the index returns to position 0.

If greater than 0, the loop is closed and the result is returned.

If the value is not greater than 0, divide the candy.

When you divide the candies, you subtract the number of candies you want to divide into the rest of the candies.

If the remaining candy is greater than 0, then there are enough candies left. Divide n candies into the current element of the result array. Candy is updated to the remaining candy remaining.

If the remaining candies are less than or equal to 0, the remaining candies are left to the current element of the result array. Candy candies are updated to 0, ending the loop, and returning the result.

To solve the code

class Solution { public int[] distributeCandies(int candies, Results int[] results = new int[num_people]; Int index = 0; Int n = 1; While (candies > 0) {int remaining = candies -n; Results [index] += n; if (Remaining > 0) { // Candies = remaining; // Candies = remaining; Results [index] += candies; // Candies = 0; } // The number of candies to be divided increases by 1 ++n; Index = index < num_people-1? Index = index < num_people-1? ++index : 0; } // Return the result array. }}Copy the code

Submit the results

The execution time was 2ms, beating 34.02% of users in time, 36.1MB in memory, and 52.71% in space.

The original link

Article link: Divide candy II