Skip to content

1103. 分糖果II

Published: at 22:49

简单模拟一下,本来想数学的,但数学全还回去了)

impl Solution {
pub fn distribute_candies(candies: i32, num_people: i32) -> Vec<i32> {
let num_people = num_people as usize;
let mut candies = candies;
let mut vec = vec![0; num_people as usize];
let mut index = 0;
let mut now = 1;
while candies >= now {
vec[index] += now;
candies -= now;
now += 1;
index = (index + 1) % (num_people as usize);
}
vec[index] += candies;
vec
}
}

Next Post
1104. 二叉树寻路