题目:原题链接(简单)
解法时间复杂度空间复杂度执行用时Ans 1 (Python) O ( N ) O(\sqrt{N}) O(N ) O ( 1 ) O(1) O(1)48ms (76.57%)Ans 2 (Python)Ans 3 (Python)LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。
解法一(情景模拟):
def distributeCandies(self, candies: int, num_people: int) -> List[int]: ans = [0 for _ in range(num_people)] num = 1 idx = 0 while candies: if idx >= num_people: idx -= num_people if candies >= num: ans[idx] += num candies -= num num += 1 idx += 1 else: ans[idx] += candies break return ans