1103. Distribute Candies to People Easy
1/**
2 * [1103] Distribute Candies to People
3 *
4 * We distribute some number of candies, to a row of n = num_people people in the following way:
5 * 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.
6 * 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.
7 * 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).
8 * Return an array (of length num_people and sum candies) that represents the final distribution of candies.
9 *
10 * Example 1:
11 *
12 * Input: candies = 7, num_people = 4
13 * Output: [1,2,3,1]
14 * Explanation:
15 * On the first turn, ans[0] += 1, and the array is [1,0,0,0].
16 * On the second turn, ans[1] += 2, and the array is [1,2,0,0].
17 * On the third turn, ans[2] += 3, and the array is [1,2,3,0].
18 * On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1].
19 *
20 * Example 2:
21 *
22 * Input: candies = 10, num_people = 3
23 * Output: [5,2,3]
24 * Explanation:
25 * On the first turn, ans[0] += 1, and the array is [1,0,0].
26 * On the second turn, ans[1] += 2, and the array is [1,2,0].
27 * On the third turn, ans[2] += 3, and the array is [1,2,3].
28 * On the fourth turn, ans[0] += 4, and the final array is [5,2,3].
29 *
30 *
31 * Constraints:
32 *
33 * 1 <= candies <= 10^9
34 * 1 <= num_people <= 1000
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/distribute-candies-to-people/
40// discuss: https://leetcode.com/problems/distribute-candies-to-people/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn distribute_candies(candies: i32, num_people: i32) -> Vec<i32> {
46 vec![]
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_1103() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.