Skip to content

Commit 4de447b

Browse files
committed
feat: add solutions to lc problem: No.1103
No.1103: Distribute Candies to People
1 parent e920be2 commit 4de447b

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

solution/1100-1199/1103.Distribute Candies to People/README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58-
直接暴力模拟发糖即可。
58+
**方法一:模拟**
59+
60+
我们可以直接模拟每一个人分到糖果的过程,按照题目描述的规则模拟即可。
61+
62+
时间复杂度 $O(\max(\sqrt{candies}, num\_people))$,空间复杂度 $O(num\_people)$。其中 $candies$ 为糖果数量。
5963

6064
<!-- tabs:start -->
6165

@@ -68,7 +72,7 @@ class Solution:
6872
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
6973
ans = [0] * num_people
7074
i = 0
71-
while candies > 0:
75+
while candies:
7276
ans[i % num_people] += min(candies, i + 1)
7377
candies -= min(candies, i + 1)
7478
i += 1
@@ -128,6 +132,19 @@ func min(a, b int) int {
128132
}
129133
```
130134

135+
### **TypeScript**
136+
137+
```ts
138+
function distributeCandies(candies: number, num_people: number): number[] {
139+
const ans: number[] = Array(num_people).fill(0);
140+
for (let i = 0; candies > 0; ++i) {
141+
ans[i % num_people] += Math.min(candies, i + 1);
142+
candies -= Math.min(candies, i + 1);
143+
}
144+
return ans;
145+
}
146+
```
147+
131148
### **...**
132149

133150
```

solution/1100-1199/1103.Distribute Candies to People/README_EN.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Solution:
5858
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
5959
ans = [0] * num_people
6060
i = 0
61-
while candies > 0:
61+
while candies:
6262
ans[i % num_people] += min(candies, i + 1)
6363
candies -= min(candies, i + 1)
6464
i += 1
@@ -116,6 +116,19 @@ func min(a, b int) int {
116116
}
117117
```
118118

119+
### **TypeScript**
120+
121+
```ts
122+
function distributeCandies(candies: number, num_people: number): number[] {
123+
const ans: number[] = Array(num_people).fill(0);
124+
for (let i = 0; candies > 0; ++i) {
125+
ans[i % num_people] += Math.min(candies, i + 1);
126+
candies -= Math.min(candies, i + 1);
127+
}
128+
return ans;
129+
}
130+
```
131+
119132
### **...**
120133

121134
```

solution/1100-1199/1103.Distribute Candies to People/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution:
22
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
33
ans = [0] * num_people
44
i = 0
5-
while candies > 0:
5+
while candies:
66
ans[i % num_people] += min(candies, i + 1)
77
candies -= min(candies, i + 1)
88
i += 1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function distributeCandies(candies: number, num_people: number): number[] {
2+
const ans: number[] = Array(num_people).fill(0);
3+
for (let i = 0; candies > 0; ++i) {
4+
ans[i % num_people] += Math.min(candies, i + 1);
5+
candies -= Math.min(candies, i + 1);
6+
}
7+
return ans;
8+
}

0 commit comments

Comments
 (0)