Skip to content

Commit a6eb1ad

Browse files
committed
leetcode
1 parent 58834cb commit a6eb1ad

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
3+
-* 1282. Group the People Given the Group Size They Belong To *-
4+
5+
There are n people that are split into some unknown number of groups. Each person is labeled with a unique ID from 0 to n - 1.
6+
7+
You are given an integer array groupSizes, where groupSizes[i] is the size of the group that person i is in. For example, if groupSizes[1] = 3, then person 1 must be in a group of size 3.
8+
9+
Return a list of groups such that each person i is in a group of size groupSizes[i].
10+
11+
Each person should appear in exactly one group, and every person must be in a group. If there are multiple answers, return any of them. It is guaranteed that there will be at least one valid solution for the given input.
12+
13+
14+
15+
Example 1:
16+
17+
Input: groupSizes = [3,3,3,3,3,1,3]
18+
Output: [[5],[0,1,2],[3,4,6]]
19+
Explanation:
20+
The first group is [5]. The size is 1, and groupSizes[5] = 1.
21+
The second group is [0,1,2]. The size is 3, and groupSizes[0] = groupSizes[1] = groupSizes[2] = 3.
22+
The third group is [3,4,6]. The size is 3, and groupSizes[3] = groupSizes[4] = groupSizes[6] = 3.
23+
Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].
24+
Example 2:
25+
26+
Input: groupSizes = [2,1,3,3,3,2]
27+
Output: [[1],[0,5],[2,3,4]]
28+
29+
30+
Constraints:
31+
32+
groupSizes.length == n
33+
1 <= n <= 500
34+
1 <= groupSizes[i] <= n
35+
36+
37+
*/
38+
39+
import 'dart:collection';
40+
41+
class A {
42+
List<List<int>> groupThePeople(List<int> groupSizes) {
43+
final HashMap<int, List<int>> hm = HashMap();
44+
final List<List<int>> res = [];
45+
46+
for (int person = 0; person < groupSizes.length; person++) {
47+
final int groupSize = groupSizes[person];
48+
if (!hm.containsKey(groupSize)) {
49+
hm[groupSize] = [person];
50+
} else {
51+
if (hm[groupSize]!.length < groupSize) {
52+
hm[groupSize]!.add(person);
53+
} else {
54+
res.add(hm[groupSize]!);
55+
hm[groupSize] = [person];
56+
}
57+
}
58+
}
59+
60+
for (final List<int> list in hm.values) {
61+
res.add(list);
62+
}
63+
64+
return res;
65+
}
66+
}
67+
68+
class NodeList {
69+
int val;
70+
NodeList? next;
71+
72+
NodeList(this.val, [this.next]);
73+
}
74+
75+
class LinkedList {
76+
NodeList? head;
77+
int length;
78+
79+
LinkedList()
80+
: head = null,
81+
length = 0;
82+
83+
void add(int val) {
84+
final newNode = NodeList(val);
85+
if (head == null) {
86+
head = newNode;
87+
} else {
88+
NodeList? current = head;
89+
while (current!.next != null) {
90+
current = current.next;
91+
}
92+
current.next = newNode;
93+
}
94+
length++; // Increase the length when adding a node
95+
}
96+
}
97+
98+
class Solution {
99+
List<List<int>> groupThePeople(List<int> groupSizes) {
100+
final HashMap groupMap = HashMap<int, LinkedList>();
101+
final result = <List<int>>[];
102+
103+
for (int person = 0; person < groupSizes.length; person++) {
104+
final groupSize = groupSizes[person];
105+
if (!groupMap.containsKey(groupSize)) {
106+
groupMap[groupSize] = LinkedList();
107+
}
108+
groupMap[groupSize]!.add(person);
109+
110+
if (groupMap[groupSize]!.length == groupSize) {
111+
final group = List<int>.filled(groupSize, 0);
112+
var current = groupMap[groupSize]!.head;
113+
for (var i = 0; i < groupSize; i++) {
114+
group[i] = current!.val;
115+
current = current.next;
116+
}
117+
result.add(group);
118+
groupMap.remove(groupSize);
119+
}
120+
}
121+
122+
return result;
123+
}
124+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
4+
5+
type NodeList struct {
6+
Val int
7+
Next *NodeList
8+
}
9+
10+
type LinkedList struct {
11+
Head *NodeList
12+
Length int // Add a Length field to keep track of the list length
13+
}
14+
15+
func NewLinkedList() *LinkedList {
16+
return &LinkedList{nil, 0}
17+
}
18+
19+
func (ll *LinkedList) Add(val int) {
20+
newNode := &NodeList{Val: val, Next: nil}
21+
if ll.Head == nil {
22+
ll.Head = newNode
23+
} else {
24+
current := ll.Head
25+
for current.Next != nil {
26+
current = current.Next
27+
}
28+
current.Next = newNode
29+
}
30+
ll.Length++ // Increase the length when adding a node
31+
}
32+
33+
func groupThePeople(groupSizes []int) [][]int {
34+
groupMap := make(map[int]*LinkedList)
35+
result := make([][]int, 0)
36+
37+
for person, groupSize := range groupSizes {
38+
if _, exists := groupMap[groupSize]; !exists {
39+
groupMap[groupSize] = NewLinkedList()
40+
}
41+
groupMap[groupSize].Add(person)
42+
43+
if groupMap[groupSize].Length == groupSize {
44+
group := make([]int, groupSize)
45+
current := groupMap[groupSize].Head
46+
for i := 0; i < groupSize; i++ {
47+
group[i] = current.Val
48+
current = current.Next
49+
}
50+
result = append(result, group)
51+
delete(groupMap, groupSize)
52+
}
53+
}
54+
55+
return result
56+
}
57+
58+

GroupThePeopleGivenTheGroupSizeTheyBelongTo/group_the_people_given_the_group_size_they_belong_to.md

Whitespace-only changes.

0 commit comments

Comments
 (0)