Skip to content

Commit 21fb8ae

Browse files
committed
1282. Group the People Given the Group Size They Belong To
1 parent 9b1ab98 commit 21fb8ae

File tree

1 file changed

+21
-0
lines changed
  • Group the People Given the Group Size They Belong To

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package kata
2+
3+
func groupThePeople(groupSizes []int) [][]int {
4+
buckets := make(map[int][]int, 0)
5+
dividedGroups := make([][]int, 0)
6+
for i, groupSize := range groupSizes {
7+
bucket := buckets[groupSize]
8+
if bucket == nil {
9+
bucket = make([]int, 0)
10+
}
11+
12+
bucket = append(bucket, i)
13+
if len(bucket) == groupSize {
14+
dividedGroups = append(dividedGroups, bucket)
15+
buckets[groupSize] = nil
16+
} else {
17+
buckets[groupSize] = bucket
18+
}
19+
}
20+
return dividedGroups
21+
}

0 commit comments

Comments
 (0)