Skip to content

Latest commit

 

History

History
27 lines (20 loc) · 750 Bytes

1282. 用户分组.md

File metadata and controls

27 lines (20 loc) · 750 Bytes
  • 哈希表
function groupThePeople(groupSizes: number[]): number[][] {
    const buckets: { [key: number]: number[] } = Object.create(null);

    for (let i = 0; i < groupSizes.length; ++i) {
        const bucket: number[] = buckets[groupSizes[i]] || [];
        bucket.push(i);
        buckets[groupSizes[i]] = bucket;
    }

    const results: number[][] = [];

    for (const index in buckets) {
        const bucket: number[] = buckets[index];
        for (let end = +index; end <= bucket.length; end += +index) {
            results.push(bucket.slice(end - +index, end));
        }
    }

    return results;
};