|
| 1 | +# 1282. Group the People Given the Group Size They Belong To |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Hash Table. |
| 5 | +- Similar Questions: Maximum Number of Groups With Increasing Length. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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`. |
| 10 | + |
| 11 | +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`. |
| 12 | + |
| 13 | +Return **a list of groups such that each person `i` is in a group of size `groupSizes[i]`**. |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | + |
| 18 | +Example 1: |
| 19 | + |
| 20 | +``` |
| 21 | +Input: groupSizes = [3,3,3,3,3,1,3] |
| 22 | +Output: [[5],[0,1,2],[3,4,6]] |
| 23 | +Explanation: |
| 24 | +The first group is [5]. The size is 1, and groupSizes[5] = 1. |
| 25 | +The second group is [0,1,2]. The size is 3, and groupSizes[0] = groupSizes[1] = groupSizes[2] = 3. |
| 26 | +The third group is [3,4,6]. The size is 3, and groupSizes[3] = groupSizes[4] = groupSizes[6] = 3. |
| 27 | +Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]]. |
| 28 | +``` |
| 29 | + |
| 30 | +Example 2: |
| 31 | + |
| 32 | +``` |
| 33 | +Input: groupSizes = [2,1,3,3,3,2] |
| 34 | +Output: [[1],[0,5],[2,3,4]] |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +**Constraints:** |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +- `groupSizes.length == n` |
| 43 | + |
| 44 | +- `1 <= n <= 500` |
| 45 | + |
| 46 | +- `1 <= groupSizes[i] <= n` |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +## Solution |
| 51 | + |
| 52 | +```javascript |
| 53 | +/** |
| 54 | + * @param {number[]} groupSizes |
| 55 | + * @return {number[][]} |
| 56 | + */ |
| 57 | +var groupThePeople = function(groupSizes) { |
| 58 | + var map = Array(groupSizes.length + 1).fill(0).map(() => []); |
| 59 | + var res = []; |
| 60 | + for (var i = 0; i < groupSizes.length; i++) { |
| 61 | + var size = groupSizes[i]; |
| 62 | + map[size].push(i); |
| 63 | + if (map[size].length === size) { |
| 64 | + res.push(map[size]); |
| 65 | + map[size] = []; |
| 66 | + } |
| 67 | + } |
| 68 | + return res; |
| 69 | +}; |
| 70 | +``` |
| 71 | + |
| 72 | +**Explain:** |
| 73 | + |
| 74 | +nope. |
| 75 | + |
| 76 | +**Complexity:** |
| 77 | + |
| 78 | +* Time complexity : O(n). |
| 79 | +* Space complexity : O(n). |
1 commit comments
vercel[bot] commentedon Sep 11, 2023
Successfully deployed to the following URLs:
leetcode-javascript – ./
leetcode-javascript-git-master-baffinlee.vercel.app
leetcode-javascript.vercel.app
leetcode-javascript-baffinlee.vercel.app