【leetcode】1282. Group the People Given the Group Size They Belong To
题目如下:
There are
npeople whose IDs go from0ton - 1and each person belongs exactly to one group. Given the arraygroupSizesof lengthntelling 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 == n1 <= n <= 5001 <= 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 ...
随机推荐
- Spring4学习回顾之路08- FactoryBean配置Bean
建立Student.java package com.lql.srping04; /** * @author: lql * @date: 2019.10.28 * Description: */ pu ...
- 初识numpy库
numpy是一个在Python中做科学计算的基础库,重在数值计算,也是大部分Python科学计算库的基础库,多用于在大型.多维数组上执行数值运算 numpy创建数组(矩阵): numpy中的数据类型: ...
- 后缀数组 LCP--模板题
题意: 给你S串和T串,用T串的所有前缀去匹配S串(匹配值是最长公共子串). 问你总值相加是多少. 思路: 先把两个S,T串倒过来,再拼接 S#T 合成一串,跑一下后缀数组 在排序好的rank里计算每 ...
- php栈的定义和出栈、入栈的实现
栈是线性表的一种,他的特点是后入先出,可以这么理解,栈就像一个存东西的盒子,先放进去的在最底层,后放进去的在上层,因为上层的东西把底层的东西压住了,下层的想要出去就必须把上层的先拿开才行. 定义:栈是 ...
- python病毒
介绍 今天碰到一个有趣的python病毒,在这里https://github.com/cranklin/Python-Virus/blob/master/pythonvirus.py#L37 源码 分 ...
- Elastic Search中filter的理解
在ES中,请求一旦发起,ES服务器是按照请求参数的顺序依次执行具体的搜索过滤逻辑的.如何定制请求体中的搜索过滤条件顺序,是一个经验活.类似query(指search中的query请求参数),也是搜索的 ...
- Rust 优劣势: v.s. C++ / v.s. Go(持续更新)
Rust 发展速度比 C++ 强很多.如果去翻 open-std 的故纸堆,会发现 C++ 这边有很多人(包括标准委员会的人)提了有用的提案,但后来大多不了了之或经历了非常长的时间才进入标准. > ...
- 富文本二进制转换成string
].KindExplain); ) { explainString = explainString.Substring(, explainString.IndexOf() + , explainStr ...
- EasyUI_前台js_分页
1.html: <table id="DataTb" title="客户信息" class="easyui-datagrid" sty ...
- 【原创】大叔经验分享(61)kudu rebalance报错
kudu rebalance命令报错 terminate called after throwing an instance of 'std::regex_error' what(): regex_e ...