@@ -41,21 +41,88 @@ Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].
41
41
<li><code>1 <= groupSizes[i] <= n</code></li>
42
42
</ul >
43
43
44
-
45
44
## Solutions
46
45
47
46
<!-- tabs:start -->
48
47
49
48
### ** Python3**
50
49
51
50
``` python
52
-
51
+ class Solution :
52
+ def groupThePeople (self , groupSizes : List[int ]) -> List[List[int ]]:
53
+ mp = collections.defaultdict(list )
54
+ for i, x in enumerate (groupSizes):
55
+ mp[x].append(i)
56
+ res = []
57
+ for x, indexes in mp.items():
58
+ l = len (indexes)
59
+ for i in range (0 , l, x):
60
+ res.append(indexes[i: i + x])
61
+ return res
53
62
```
54
63
55
64
### ** Java**
56
65
57
66
``` java
67
+ class Solution {
68
+ public List<List<Integer > > groupThePeople (int [] groupSizes ) {
69
+ Map<Integer , List<Integer > > mp = new HashMap<> ();
70
+ for (int i = 0 ; i < groupSizes. length; ++ i) {
71
+ mp. computeIfAbsent(groupSizes[i], k - > new ArrayList<> ()). add(i);
72
+ }
73
+ List<List<Integer > > res = new ArrayList<> ();
74
+ for (Map . Entry<Integer , List<Integer > > entry : mp. entrySet()) {
75
+ int x = entry. getKey();
76
+ List<Integer > indexes = entry. getValue();
77
+ for (int i = 0 ; i < indexes. size(); i += x) {
78
+ res. add(new ArrayList<> (indexes. subList(i, i + x)));
79
+ }
80
+ }
81
+ return res;
82
+ }
83
+ }
84
+ ```
85
+
86
+ ### ** C++**
87
+
88
+ ``` cpp
89
+ class Solution {
90
+ public:
91
+ vector<vector<int >> groupThePeople(vector<int >& groupSizes) {
92
+ unordered_map<int, vector<int >> mp;
93
+ for (int i = 0; i < groupSizes.size(); ++i) mp[ groupSizes[ i]] .push_back(i);
94
+ vector<vector<int >> res;
95
+ for (auto& entry : mp)
96
+ {
97
+ int x = entry.first;
98
+ auto indexes = entry.second;
99
+ for (int i = 0; i < indexes.size(); i += x)
100
+ {
101
+ vector<int > t(indexes.begin() + i, indexes.begin() + i + x);
102
+ res.push_back(t);
103
+ }
104
+ }
105
+ return res;
106
+ }
107
+ };
108
+ ```
58
109
110
+ ### **Go**
111
+
112
+ ```go
113
+ func groupThePeople(groupSizes []int) [][]int {
114
+ mp := make(map[int][]int)
115
+ for i, x := range groupSizes {
116
+ mp[x] = append(mp[x], i)
117
+ }
118
+ var res [][]int
119
+ for x, indexes := range mp {
120
+ for i := 0; i < len(indexes); i += x {
121
+ res = append(res, indexes[i:i+x])
122
+ }
123
+ }
124
+ return res
125
+ }
59
126
```
60
127
61
128
### ** ...**
0 commit comments