forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1103.java
34 lines (32 loc) · 1.12 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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _1103 {
public static class Solution1 {
public int[] distributeCandies(int candies, int numPeople) {
Map<Integer, Integer> map = new HashMap<>();
int candy = 1;
while (candies > 0) {
for (int person = 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 = new int[numPeople];
for (int i = 1; i <= numPeople; i++) {
if (map.containsKey(i)) {
result[i - 1] = map.get(i);
} else {
result[i - 1] = 0;
}
}
return result;
}
}
}