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,每个人只属于一个组,给定一个长度为n的数组gs,gs[i]表示编号为i的人所在组的人数,返回任意一个可能的分组。题目保证存在至少一种分组。

思路:每个人可以独立成组,也可以所有人都在一个组,所以可能的组的人数为1-n,我们把 组的人数相同 的人的编号放在同一个组group,最后根据组的人数进行分割分别成组(或者每当这个组达到上限,就把组内所有人放在最终的组,把当前组清空)

c++:

 class Solution {
public:
vector<vector<int>> groupThePeople(vector<int>& gs) {
// vector<vector<int>> res;
// //unordered_map<int, vector<int>> G;
// vector<vector<int>> G(gs.size() + 1);
// for (int i = 0; i < gs.size(); ++i) {
// G[gs[i]].push_back(i);
// if (G[gs[i]].size() == gs[i]) {
// res.push_back(G[gs[i]]);
// G[gs[i]].clear();
// }
// }
// return res;
vector<vector<int>> res;
unordered_map<int,vector<int>> mp;
for (int i = ; i < gs.size(); ++i) {
mp[gs[i]].push_back(i);
}
for (auto x : mp) {
int i = x.first; //i表示组的人数
vector<int> v = x.second; //可以放在同一个组的所有人的编号
vector<int> t(i); //每i个人放一个组
for (int j = ; j < v.size(); ++j) {
t[j % i] = v[j];
if ((j + ) % i == ) {
res.push_back(t);
}
}
}
return res;
}
};

python3:

 class Solution:
def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:
count = collections.defaultdict(list)
for i, size in enumerate(groupSizes):
count[size].append(i)
return [v[i:i+size] for size, v in count.items() for i in range(, len(v), size)]

leetcode 1282. Group the People Given the Group Size They Belong To的更多相关文章

  1. 【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. ...

  2. LeetCode 49. 字母异位词分组(Group Anagrams)

    题目描述 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "ta ...

  3. sql的 group by 分组;linq的 group by 分组

    先来看看 linq的,下面的一段linq 是 ,在 学生导入数据的时候,我们根据学生的手机号码和学生名称进行分组,如果有重复的,我们就筛选出来,用到了 linq的 group by,注意这里是new出 ...

  4. mysql 数据操作 单表查询 group by 聚合函数 没有group by情况下

    聚合函数只能用在组里使用 #没有group by 则默认算作一组 取出所有员工的最高工资 mysql> select max(salary) from employee; +---------- ...

  5. 1.(group by)如何让group by分组后,每组中的所有数据都显示出来

    问题描述:表如下,如何让这个表按device_id这个字段分组,且组中的每条数据都查寻出来?(假如说这个表名为:devicedata) 错误答案:select * from devicedata GR ...

  6. [LeetCode] Search in a Sorted Array of Unknown Size 在未知大小的有序数组中搜索

    Given an integer array sorted in ascending order, write a function to search target in nums.  If tar ...

  7. [ext4]03 磁盘布局 – Flexible group分析

    Flexible Block Groups (flex_bg),我称之为"弹性块组",是EXT4文件系统引入的一个feature. 所谓Flexible Block Groups, ...

  8. Oracle学习笔记五 SQL命令(三):Group by、排序、连接查询、子查询、分页

    GROUP BY和HAVING子句 GROUP BY子句 用于将信息划分为更小的组每一组行返回针对该组的单个结果 --统计每个部门的人数: Select count(*) from emp group ...

  9. MongoDB聚合运算之group和aggregate聚集框架简单聚合(10)

    聚合运算之group 语法: db.collection.group( { key:{key1:1,key2:1}, cond:{}, reduce: function(curr,result) { ...

随机推荐

  1. LeetCode 470. 用 Rand7() 实现 Rand10()(Implement Rand10() Using Rand7())

    题目描述 已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数. 不要使用系统的 Math.random() 方法. 示 ...

  2. 在Ubuntu 16.04配置VNC Server (灰屏问题解决)

      使用命令安装 sudo apt install xfce4 xfce4-goodies tightvncserver 编辑vnc启动文件,安全期间最好备份一下 mv ~/.vnc/xstartup ...

  3. Appium+Robotframework实现iOS应用的自动化测试

    Appium+Robotframework实现iOS应用的自动化测试 连接地址: 地址:https://blog.csdn.net/wd168/article/month/2016/06 1.http ...

  4. PHP AJAX 返回JSON 数据

    例子:利用AJAX返回JSON数据,间接访问数据库,查出Nation 表,并用下拉列表显示 造一个外部下拉列表框 </select> JQurey代码 $(document).ready( ...

  5. 给JAVA的eclipse IDE 在线安装 SVN插件 / 给 eclipse 添加打开所在的文件夹功能

    http://subclipse.tigris.org/servlets/ProjectProcess?pageID=p4wYuA 首先,在这个网址找着最新在线安装链接 就是那个 Links for ...

  6. GitHub:Python

    ylbtech-GitHub:Python 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://github.com/python 2.   6 ...

  7. Windows 下的SSH客户端

    在日常Linux系统管理中,会使用SSH工具连接服务器,之所以SSH连接主要是为了安全,传统的telnet连接方式是以明文传输,很不安全,网络中如果又热窃听抓包,密码将要泄露.在众多SSH连接中,Pu ...

  8. 作为web开发人员,你必须要知道的问题! (持续更新)

    GET 和 POST 的区别 GET请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:/test/demo_form.asp?name1=value1&name2=val ...

  9. RESTful 介绍

    什么是RESTful?一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务端交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. ...

  10. Python之数据库操纵工具

    前言 基于原生模块:pymysql 推荐教程 Python 数据库处理的类库 pymysql [python原生模块] SQLAchemy [ORM框架] Python3 MySQL 数据库连接 - ...