LeetCode-1103. Sub-Candy II (Golang implementation)

LeetCode Question Number: 1103. Points Candy II

Sit in rows and divide the candies.

We bought some candy candies and plan to distribute them to n = num_people children in the queue.

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

Then, we go back to the starting point of the team and give the first child n + 1 candies, the second child n + 2, and so on, until we give the last child 2 * n candies.

Repeat the above process (each time one more candy is given than the last time, and when the end of the line is reached, start again from the beginning of the line), until we have divided all the candies. Note that even if the number of candies left in our hands is not enough (not more than the number of candies sent last time), all of these candies will be distributed to the current children.

Return an array whose length is num_people and the sum of elements is candies to indicate the final distribution of candies (that is, ans[i] represents the number of candies allocated by the i-th child).

 Example 1:

Input: candies = 7, num_people = 4
Output: [1,2,3,1]
Explanation: For the
first time, ans[0] += 1, the array becomes [1,0,0,0].
The second time, ans[1] += 2, the array becomes [1,2,0,0].
The third time, ans[2] += 3, the array becomes [1,2,3,0].
The fourth time, ans[3] += 1 (because there is only 1 candy left at this time), the final array becomes [1,2,3,1].
Example 2:

Input: candies = 10, num_people = 3
Output: [5,2,3]
Explanation: For the
first time, ans[0] += 1, the array becomes [1,0,0].
The second time, ans[1] += 2, the array becomes [1,2,0].
The third time, ans[2] += 3, the array becomes [1,2,3].
The fourth time, ans[0] += 4, the final array becomes [5,2,3].

 Problem-solving ideas:

The most intuitive method is to continuously traverse the array, and keep dividing if there is sugar until there is no sugar.

 Code:

func distributeCandies(candies int, num_people int) []int {
	ans := make([]int, num_people)
	//表示当前所需发的糖果数
	count := 0
	//记录遍历次数,方便下标循环
	index := 0
	// 只要糖还有,就继续分
	for candies > 0 {
		count++
		//返回的数会一直是相应数组的下标
		i := index % num_people
		//足够比前一数量加一大
		if candies > count {
			ans[i] += count
		} else {
			//不够,则直接把剩余糖果全分,循环结束
			ans[i] += candies
		}
		//往下分
		index++
		//剩余糖果数
		candies -= count
	}
	return ans
}

Past review:

[1] LeetCode-409. The longest palindrome (implemented by Goland)

[2] LeetCode-459. Repeated substring (implemented by Goland)

[3] LeetCode-53. Maximum subsequence sum (implemented by Goland)


❤If the article is helpful to you, please click like at the top right corner of the article or at the end of the article! (づ ̄ 3 ̄)づ 

❤If you like the articles shared by the white rabbit, please pay attention to the white rabbit! (๑′ᴗ‵๑)づ╭❤~

❤If you have any questions about the article, please leave a message below or join the group to discuss [group number: 708072830]

❤In view of the limited personal experience, all opinions and technical research points, if you have any objections, please reply directly to the discussion (do not make offensive remarks)

Guess you like

Origin blog.csdn.net/weixin_43970743/article/details/109001046