【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 ...
随机推荐
- oracle——学习之路(oracle内置函数)
oracle与很多内置函数,主要分为单行函数与集合函数. 首先要提一下dual表,它oracle的一个表,没有什么实质的东西,不能删除它,否则会造成Oracle无法启动等问题,他有很大用处,可以利用它 ...
- Kafka如何实现每秒上百万的高并发写入
Kafka是高吞吐低延迟的高并发.高性能的消息中间件,在大数据领域有极为广泛的运用.配置良好的Kafka集群甚至可以做到每秒几十万.上百万的超高并发写入. 那么Kafka到底是如何做到这么高的吞吐量和 ...
- Java实现循环队列
一.分析 队列是一种先进先出的线性表,它只允许在表的一端进行插入,而在另一端删除元素.允许插入的一端称为队尾,允许删除的一端称为队头. 循环队列是一种以顺序存储结构表示的队列,为了解决“假溢出”问题而 ...
- python爬虫实战--抖音
申明&警告: 请在相关网站的许可范围内爬取数据.以免影响网站正常运行, 如果我的文章有触犯权益的地方, 请告知删除. 上一篇爬取知乎的文章基本就是大多数网站的爬取思路了(headers部分其实 ...
- 开发过程遇到的css样式问题记录
一.移动端 1.部分安卓机圆角border-radius失效,显示为方形状? background-clip: padding-box; 2.部分安卓机字体图标出现锯齿? 使用iconfont图标 ...
- C#面向对象19 值传递和引用传递
值类型:int double char decimal bool enum struct引用类型:string 数组 自定义类 集合 object 接口 **值传递和引用传递1.值类型在复制的时候,传 ...
- 解决VS2005打开js,css,asp.php等文件,中文都是乱码的问题
用记事本打开可以正常观看但是用VS2005编辑器打开JS,中文确实乱码. 解决办法:在VS 2005 的设置里面选择自动检测Utf-8:“工具”->“选项”->“文本编辑器”->“自 ...
- 解决windows系统下ping,ipconfig不是内部或外部命令
一般情况下,都是误删了系统变量path的值.解决方法:右击我的电脑 → 选择属性 → 选择高级系统设置 → 环境变量 → 在系统变量列表中,找到“path”环境变量双击,打开.在变量值这一栏检测下是否 ...
- Laravel使用whereHas进行过滤不符合条件的预加载with数据
问题描述:目前有用户表,文章表,文章评论表,收藏表.我需要获我的收藏文章列表(可以被搜索,通过分类,文章标题等),通过收藏预加载with文章表,文章评论表,文章用户表 解决办法:通过whereHas限 ...
- 99乘法表(js)
//九九乘法表 let i,j,str; for(i=1;i<=9;i++) { str = ""; for(j=1;j<=i;j++) { str = str+i+' ...