题目如下:

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

解题思路:先按照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的更多相关文章

  1. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  2. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  3. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LeetCode】851. Loud and Rich 解题报告(Python)

    [LeetCode]851. Loud and Rich 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  5. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  6. 【LeetCode】468. Validate IP Address 解题报告(Python)

    [LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  7. 【LeetCode】809. Expressive Words 解题报告(Python)

    [LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. KUDU数据导入尝试一:TextFile数据导入Hive,Hive数据导入KUDU

    背景 SQLSERVER数据库中单表数据几十亿,分区方案也已经无法查询出结果.故:采用导出功能,导出数据到Text文本(文本>40G)中. 因上原因,所以本次的实验样本为:[数据量:61w条,文 ...

  2. jstl与EL表达式

    一·el表达式介绍 EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} ...

  3. jQuery添加/删除Select的Option项

    使用语法1. $("#select_id").append("<option value='Value'>Text</option>") ...

  4. A/B HDU-1576(简单的数论题)

    Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input 数据的第一 ...

  5. Codeforces Round #586 (Div. 1 + Div. 2) D.Alex and Julian 简单证明

    题意:在序列中删除最少元素使得得到的图是二分图. 其中点是整数域的点. 比如b1=2   那么a可以连b当且仅当|a-b|=2 同时这里的a,b是任意整数. 怎样判定一个序列是否合法呢?于是想到了二分 ...

  6. JWT的认识和session的区别

    1.前后端分离框架中前端和后端域名不同,不能跨域请求,加上移动端无cookie,所以无法使用session.2.基于token的认证和传统的session认证的区别: 传统的session认证: 我们 ...

  7. flask 接收参数小坑

    前后端分离: 1.get方式: items = dict(request.args.items()) app_name = items["app_name"].strip() 或 ...

  8. Vanya and Scales CodeForces - 552C (思维)

    大意: $101$个砝码, 重$w^0,w^1,...,w^{100}$, 求能否称出重量$m$. w<=3时显然可以称出所有重量, 否则可以暴力双端搜索. #include <iostr ...

  9. DVWA之SQL注入演练(low)

    1.设置 把安全等级先调整为low,让自己获得点信心,免得一来就被打脸. 2.测试和分析页面的功能       这里有一个输入框 根据上面的提示,输入用户的id.然后我们输入之后,发现它返回了关于这个 ...

  10. sql循环(WITH AS短语也叫做子查询部分)

    --表结构 SELECT id,position,Parentid FROM op_client_sales_structure WITH TEST_CTE AS ( SELECT id,positi ...