Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e5fca16

Browse files
committed
feat: add solutions to lc problem: No.1282.Group the People Given the Group Size They Belong To
1 parent 3d2ce73 commit e5fca16

File tree

6 files changed

+198
-4
lines changed

6 files changed

+198
-4
lines changed

‎solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
<li><code>1 &lt;=&nbsp;groupSizes[i] &lt;= n</code></li>
3737
</ul>
3838

39-
4039
## 解法
4140

4241
<!-- 这里可写通用的实现逻辑 -->
@@ -48,15 +47,83 @@
4847
<!-- 这里可写当前语言的特殊实现逻辑 -->
4948

5049
```python
51-
50+
class Solution:
51+
def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:
52+
mp = collections.defaultdict(list)
53+
for i, x in enumerate(groupSizes):
54+
mp[x].append(i)
55+
res = []
56+
for x, indexes in mp.items():
57+
l = len(indexes)
58+
for i in range(0, l, x):
59+
res.append(indexes[i: i + x])
60+
return res
5261
```
5362

5463
### **Java**
5564

5665
<!-- 这里可写当前语言的特殊实现逻辑 -->
5766

5867
```java
68+
class Solution {
69+
public List<List<Integer>> groupThePeople(int[] groupSizes) {
70+
Map<Integer, List<Integer>> mp = new HashMap<>();
71+
for (int i = 0; i < groupSizes.length; ++i) {
72+
mp.computeIfAbsent(groupSizes[i], k -> new ArrayList<>()).add(i);
73+
}
74+
List<List<Integer>> res = new ArrayList<>();
75+
for (Map.Entry<Integer, List<Integer>> entry : mp.entrySet()) {
76+
int x = entry.getKey();
77+
List<Integer> indexes = entry.getValue();
78+
for (int i = 0; i < indexes.size(); i += x) {
79+
res.add(new ArrayList<>(indexes.subList(i, i + x)));
80+
}
81+
}
82+
return res;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
93+
unordered_map<int, vector<int>> mp;
94+
for (int i = 0; i < groupSizes.size(); ++i) mp[groupSizes[i]].push_back(i);
95+
vector<vector<int>> res;
96+
for (auto& entry : mp)
97+
{
98+
int x = entry.first;
99+
auto indexes = entry.second;
100+
for (int i = 0; i < indexes.size(); i += x)
101+
{
102+
vector<int> t(indexes.begin() + i, indexes.begin() + i + x);
103+
res.push_back(t);
104+
}
105+
}
106+
return res;
107+
}
108+
};
109+
```
59110
111+
### **Go**
112+
113+
```go
114+
func groupThePeople(groupSizes []int) [][]int {
115+
mp := make(map[int][]int)
116+
for i, x := range groupSizes {
117+
mp[x] = append(mp[x], i)
118+
}
119+
var res [][]int
120+
for x, indexes := range mp {
121+
for i := 0; i < len(indexes); i += x {
122+
res = append(res, indexes[i:i+x])
123+
}
124+
}
125+
return res
126+
}
60127
```
61128

62129
### **...**

‎solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,88 @@ Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].
4141
<li><code>1 &lt;=&nbsp;groupSizes[i] &lt;= n</code></li>
4242
</ul>
4343

44-
4544
## Solutions
4645

4746
<!-- tabs:start -->
4847

4948
### **Python3**
5049

5150
```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
5362
```
5463

5564
### **Java**
5665

5766
```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+
```
58109
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+
}
59126
```
60127

61128
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
4+
unordered_map<int, vector<int>> mp;
5+
for (int i = 0; i < groupSizes.size(); ++i) mp[groupSizes[i]].push_back(i);
6+
vector<vector<int>> res;
7+
for (auto& entry : mp)
8+
{
9+
int x = entry.first;
10+
auto indexes = entry.second;
11+
for (int i = 0; i < indexes.size(); i += x)
12+
{
13+
vector<int> t(indexes.begin() + i, indexes.begin() + i + x);
14+
res.push_back(t);
15+
}
16+
}
17+
return res;
18+
}
19+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func groupThePeople(groupSizes []int) [][]int {
2+
mp := make(map[int][]int)
3+
for i, x := range groupSizes {
4+
mp[x] = append(mp[x], i)
5+
}
6+
var res [][]int
7+
for x, indexes := range mp {
8+
for i := 0; i < len(indexes); i += x {
9+
res = append(res, indexes[i:i+x])
10+
}
11+
}
12+
return res
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public List<List<Integer>> groupThePeople(int[] groupSizes) {
3+
Map<Integer, List<Integer>> mp = new HashMap<>();
4+
for (int i = 0; i < groupSizes.length; ++i) {
5+
mp.computeIfAbsent(groupSizes[i], k -> new ArrayList<>()).add(i);
6+
}
7+
List<List<Integer>> res = new ArrayList<>();
8+
for (Map.Entry<Integer, List<Integer>> entry : mp.entrySet()) {
9+
int x = entry.getKey();
10+
List<Integer> indexes = entry.getValue();
11+
for (int i = 0; i < indexes.size(); i += x) {
12+
res.add(new ArrayList<>(indexes.subList(i, i + x)));
13+
}
14+
}
15+
return res;
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:
3+
mp = collections.defaultdict(list)
4+
for i, x in enumerate(groupSizes):
5+
mp[x].append(i)
6+
res = []
7+
for x, indexes in mp.items():
8+
l = len(indexes)
9+
for i in range(0, l, x):
10+
res.append(indexes[i: i + x])
11+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /