Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-09-11 17:35:41
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/description/
1282. Group the People Given the Group Size They Belong To
给你一个 size 为 n 的阵列 groupSizes 表示编号 0 ~ n-1 的人,groupSizes[i] 表
示编号为 i 的人在的组别有几个人,返回一个列表,这个列表表示了 n 个人被分成对
应人数的组别,题目保证至少有一解,组别顺序随意。
Example 1:
Input: groupSizes = [3,3,3,3,3,1,3]
Output: [[5],[0,1,2],[3,4,6]]
Explanation:
The first group is [5]. The size is 1, and groupSizes[5] = 1.
The second group is [0,1,2]. The size is 3, and groupSizes[0] = groupSizes[1]
= groupSizes[2] = 3.
The third group is [3,4,6]. The size is 3, and groupSizes[3] = groupSizes[4]
= groupSizes[6] = 3.
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]]
思路:
1.利用一个 map 纪录 key = 这个组的目标人数 value = 这个组的成员。
2.每次都往目标人数的列表添加成员,当满足人数的时候表示分组完毕,将map清空,
并把组的成员加入结果集。
Java Code:
作者: sustainer123 (caster)   2023-09-11 17:45:00
大师
作者: JerryChungYC (JerryChung)   2023-09-11 18:34:00
数字由小到大 再根据数字大小依序放进去吗

Links booklink

Contact Us: admin [ a t ] ucptt.com