【leetcode】1282. Group the People Given the Group Size They Belong To
题目如下:
There are
n
people whose IDs go from0
ton - 1
and each person belongs exactly to one group. Given the arraygroupSizes
of lengthn
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
解题思路:先按照groupsize把人分组,然后再每个groupsize里面的人根据groupsize分成指定个小组。
代码如下:
class Solution(object):
def groupThePeople(self, groupSizes):
"""
:type groupSizes: List[int]
:rtype: List[List[int]]
"""
dic = {}
for i in range(len(groupSizes)):
key = groupSizes[i]
dic[key] = dic.setdefault(key,[]) + [i]
res = []
for key in dic.iterkeys():
inx = 0
while inx + key <= len(dic[key]):
res.append(dic[key][inx:inx+key])
inx += key
return res
【leetcode】1282. Group the People Given the Group Size They Belong To的更多相关文章
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】851. Loud and Rich 解题报告(Python)
[LeetCode]851. Loud and Rich 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】809. Expressive Words 解题报告(Python)
[LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- centerOS7安装lnmp环境
视频地址: https://www.bilibili.com/video/av55251610?p=65 安装nginx http://nginx.org 点击 download vim /etc/y ...
- MYSQL---触发器简单了解
触发器 trigger 1.触发器是指事先为某张表绑定一段代码,当表中某些内容发生改变(增insert.删delete.改update)时,系统自动触发绑定的那段代码并执行.比如 一旦订单表里插入新订 ...
- Web前端开发JavaScript提高
JavaScript 一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型,它的解释器被称为JavaScript引擎,是浏览器的一部分,并且是被广泛用于客户端的脚本语言,JavaS ...
- 树莓派安装使用RXTX
在RaspberryPi树莓派上使用RXTX(RXTX的源码安装)Linux 编译RXTX(JAVA串口开发)源码 如果为windows系统,则使用rxtx比较简单,到http://fizzed.co ...
- 清除vs2005、vs2008起始页最近打开项目
有时候vs2005起始最近打开项目过多很想清除掉,但打遍了也没找到清除选项在哪里,今天找到了方法,发上来和大家共享. 方法一手工操作方法:1)删除最近打开的文件运行regedit,打开HKEY_CUR ...
- winfrom 界面时间动态加载
Timer time1 = new Timer(); private void time1_Tick(object sender, EventArgs e) { lTime.Text = DateTi ...
- 消息队列: rabbitMQ
什么是rabbitMQ? rabbitMQ是一款基于AMQP协议的消息中间件,它能够在应用之间提供可靠的消息传输.在易用性,扩展性,高可用性上表现优秀.而且使用消息中间件利于应用之间的解耦,生产者(客 ...
- JS基础_逻辑运算符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Java学习 面向对象(下)——Java疯狂讲义th4
面向对象(下) [TOC] 包装类 通过包装类可以把8个基本类型的值包装成对象使用. 自动拆箱.自动装箱 把字符串类型值转换成基本类型的值: 包装类的 parseXxx(String s)静态方法 包 ...
- Spring Boot WebFlux整合mongoDB
引入maven文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...