|
| 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 | +} |
0 commit comments