/*
* @lc app=leetcode.cn id=1282 lang=typescript
*
* [1282] 用户分组
*/
// @lc code=start
function groupThePeople(groupSizes: number[]): number[][] {}
// @lc code=end
function groupThePeople(groupSizes: number[]): number[][] {
const map = new Map<number, number[][]>()
for (let [i, size] of groupSizes.entries()) {
if (!map.has(size)) map.set(size, [])
let arr = map.get(size)!
if (!arr.length || arr[arr.length - 1].length === size) arr.push([])
arr[arr.length - 1].push(i)
}
const res: number[][] = []
for (let [, groups] of map) {
for (let group of groups) res.push(group)
}
return res
}
test.each([
{ input: { groupSizes: [3, 3, 3, 3, 3, 1, 3] }, output: [[5], [0, 1, 2], [3, 4, 6]] },
{ input: { groupSizes: [2, 1, 3, 3, 3, 2] }, output: [[1], [0, 5], [2, 3, 4]] },
])('input: groupSizes = $input.groupSizes', ({ input: { groupSizes }, output }) => {
expect(groupThePeople(groupSizes)).toIncludeSameMembers(output)
})