- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1103.java
34 lines (32 loc) · 1.14 KB
/
_1103.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashMap;
importjava.util.Map;
publicclass_1103 {
publicstaticclassSolution1 {
publicint[] distributeCandies(intcandies, intnumPeople) {
Map<Integer, Integer> map = newHashMap<>();
intcandy = 1;
while (candies > 0) {
for (intperson = 1; person <= numPeople && candies > 0; person++, candy++) {
if (candies < candy) {
map.put(person, map.getOrDefault(person, 0) + candies);
candies -= candy;
break;
} else {
map.put(person, map.getOrDefault(person, 0) + candy);
candies -= candy;
}
}
}
int[] result = newint[numPeople];
for (inti = 1; i <= numPeople; i++) {
if (map.containsKey(i)) {
result[i - 1] = map.get(i);
} else {
result[i - 1] = 0;
}
}
returnresult;
}
}
}