Skip to content

Commit fc562a4

Browse files
committed
Add solution #1282
1 parent eeb5213 commit fc562a4

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,395 LeetCode solutions in JavaScript
1+
# 1,396 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -974,6 +974,7 @@
974974
1276|[Number of Burgers with No Waste of Ingredients](./solutions/1276-number-of-burgers-with-no-waste-of-ingredients.js)|Medium|
975975
1277|[Count Square Submatrices with All Ones](./solutions/1277-count-square-submatrices-with-all-ones.js)|Medium|
976976
1278|[Palindrome Partitioning III](./solutions/1278-palindrome-partitioning-iii.js)|Hard|
977+
1282|[Group the People Given the Group Size They Belong To](./solutions/1282-group-the-people-given-the-group-size-they-belong-to.js)|Medium|
977978
1284|[Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](./solutions/1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js)|Hard|
978979
1286|[Iterator for Combination](./solutions/1286-iterator-for-combination.js)|Medium|
979980
1287|[Element Appearing More Than 25% In Sorted Array](./solutions/1287-element-appearing-more-than-25-in-sorted-array.js)|Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 1282. Group the People Given the Group Size They Belong To
3+
* https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/
4+
* Difficulty: Medium
5+
*
6+
* There are n people that are split into some unknown number of groups. Each person is labeled
7+
* with a unique ID from 0 to n - 1.
8+
*
9+
* You are given an integer array groupSizes, where groupSizes[i] is the size of the group that
10+
* person i is in. For example, if groupSizes[1] = 3, then person 1 must be in a group of size 3.
11+
*
12+
* Return a list of groups such that each person i is in a group of size groupSizes[i].
13+
*
14+
* Each person should appear in exactly one group, and every person must be in a group. If there
15+
* are multiple answers, return any of them. It is guaranteed that there will be at least one
16+
* valid solution for the given input.
17+
*/
18+
19+
/**
20+
* @param {number[]} groupSizes
21+
* @return {number[][]}
22+
*/
23+
var groupThePeople = function(groupSizes) {
24+
const map = new Map();
25+
const grouped = [];
26+
27+
for (let i = 0; i < groupSizes.length; i++) {
28+
const size = groupSizes[i];
29+
30+
map.set(size, map.has(size) ? [...map.get(size), i] : [i]);
31+
32+
if (map.get(size).length === size) {
33+
grouped.push(map.get(size));
34+
map.delete(size);
35+
}
36+
}
37+
38+
return grouped;
39+
};

0 commit comments

Comments
 (0)