[LeetCode] 1282. Group the People Given the Group Size They Belong To

There are n people whose IDs go from 0 to n - 1 and each person belongs exactly to one group. Given the array groupSizes of length n telling the group size each person belongs to, return the groups there are and the people's IDs each group includes.

You can return any solution in any order and the same applies for IDs. Also, it is guaranteed that there exists at least one solution. 

Example 1:

Input: groupSizes = [3,3,3,3,3,1,3]
Output: [[5],[0,1,2],[3,4,6]]
Explanation: 
Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].

Example 2:

Input: groupSizes = [2,1,3,3,3,2]
Output: [[1],[0,5],[2,3,4]]

Constraints:

  • groupSizes.length == n
  • 1 <= n <= 500
  • 1 <= groupSizes[i] <= n

用户分组。

有 n 个人被分成数量未知的组。每个人都被标记为一个从 0 到 n - 1 的唯一ID 。

给定一个整数数组 groupSizes ,其中 groupSizes[i] 是第 i 个人所在的组的大小。例如,如果 groupSizes[1] = 3 ,则第 1 个人必须位于大小为 3 的组中。

返回一个组列表,使每个人 i 都在一个大小为 groupSizes[i] 的组中。

每个人应该 恰好只 出现在 一个组 中,并且每个人必须在一个组中。如果有多个答案,返回其中 任何 一个。可以 保证 给定输入 至少有一个 有效的解。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/group-the-people-given-the-group-size-they-belong-to
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是用 hashmap,key 存某个分组的大小,value 是一个 list,list 里面存的是每个人的 index。举个例子,比如第一个例子,当我遇到第一个 3 的时候,我知道 index 为 0 的人是被分到了一个三人组;同理,index 为 1,2,3,4,6 的人也都被分到了一个三人组,但是 index 为 5 的那个人是自己一个人一组的。遍历 input,当遇到同样的 groupSizes[i] 的时候,就将 i 放入 hashmap 对应的 key 的 list 中。同时注意,每次加入之后记得看一下当前 list 的大小是否等于当前的 key 了,意思是看一下当前 list 里面的人数是否已经达到 groupSizes[i],若达到了,则说明有一个分组已经确定,可以加入结果集,同时记得将 map 里这个 entry 整个删掉,否则会报错。举个例子,还是第一个例子,当你遍历到前三个 3 的时候,其实你已经确定好了第一个分组,如果此时你不删除这个分组,那么后面的 3 也会被加入 map 里 key 为 3 的那个 entry 里面,导致结果错误。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public List<List<Integer>> groupThePeople(int[] groupSizes) {
 3         Map<Integer, List<Integer>> map = new HashMap<>();
 4         List<List<Integer>> res = new ArrayList<>();
 5         for (int i = 0; i < groupSizes.length; i++) {
 6             if (!map.containsKey(groupSizes[i])) {
 7                 map.put(groupSizes[i], new ArrayList<>());
 8             }
 9             List<Integer> cur = map.get(groupSizes[i]);
10             cur.add(i);
11             if (cur.size() == groupSizes[i]) {
12                 res.add(cur);
13                 map.remove(groupSizes[i]);
14             }
15         }
16         return res;
17     }
18 }

 

JavaScript实现

 1 /**
 2  * @param {number[]} groupSizes
 3  * @return {number[][]}
 4  */
 5 var groupThePeople = function(groupSizes) {
 6     let res = [];
 7     let map = new Map();
 8     for (let i = 0; i < groupSizes.length; i++) {
 9         if (!map.has(groupSizes[i])) {
10             map.set(groupSizes[i], []);
11         }
12         let cur = map.get(groupSizes[i]);
13         cur.push(i);
14         if (cur.length == groupSizes[i]) {
15             res.push(cur);
16             map.delete(groupSizes[i]);
17         }
18     }
19     return res;
20 };

 

LeetCode 题目总结

posted @ 2020-06-26 07:01  CNoodle  阅读(315)  评论(0编辑  收藏  举报