Skip to content

Commit 1caabc6

Browse files
committed
Merge branch 'master' of github.com:BaffinLee/leetcode-javascript
2 parents c231707 + bfd6fcd commit 1caabc6

2 files changed

+182
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 1282. Group the People Given the Group Size They Belong To
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Hash Table.
5+
- Similar Questions: Maximum Number of Groups With Increasing Length.
6+
7+
## Problem
8+
9+
There are `n` people that are split into some unknown number of groups. Each person is labeled with a **unique ID** from `0` to `n - 1`.
10+
11+
You are given an integer array `groupSizes`, where `groupSizes[i]` is the size of the group that person `i` is in. For example, if `groupSizes[1] = 3`, then person `1` must be in a group of size `3`.
12+
13+
Return **a list of groups such that each person `i` is in a group of size `groupSizes[i]`**.
14+
15+
Each person should appear in **exactly one group**, and every person must be in a group. If there are multiple answers, **return any of them**. It is **guaranteed** that there will be **at least one** valid solution for the given input.
16+
17+
 
18+
Example 1:
19+
20+
```
21+
Input: groupSizes = [3,3,3,3,3,1,3]
22+
Output: [[5],[0,1,2],[3,4,6]]
23+
Explanation:
24+
The first group is [5]. The size is 1, and groupSizes[5] = 1.
25+
The second group is [0,1,2]. The size is 3, and groupSizes[0] = groupSizes[1] = groupSizes[2] = 3.
26+
The third group is [3,4,6]. The size is 3, and groupSizes[3] = groupSizes[4] = groupSizes[6] = 3.
27+
Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].
28+
```
29+
30+
Example 2:
31+
32+
```
33+
Input: groupSizes = [2,1,3,3,3,2]
34+
Output: [[1],[0,5],[2,3,4]]
35+
```
36+
37+
 
38+
**Constraints:**
39+
40+
41+
42+
- `groupSizes.length == n`
43+
44+
- `1 <= n <= 500`
45+
46+
- `1 <= groupSizes[i] <= n`
47+
48+
49+
50+
## Solution
51+
52+
```javascript
53+
/**
54+
* @param {number[]} groupSizes
55+
* @return {number[][]}
56+
*/
57+
var groupThePeople = function(groupSizes) {
58+
var map = Array(groupSizes.length + 1).fill(0).map(() => []);
59+
var res = [];
60+
for (var i = 0; i < groupSizes.length; i++) {
61+
var size = groupSizes[i];
62+
map[size].push(i);
63+
if (map[size].length === size) {
64+
res.push(map[size]);
65+
map[size] = [];
66+
}
67+
}
68+
return res;
69+
};
70+
```
71+
72+
**Explain:**
73+
74+
nope.
75+
76+
**Complexity:**
77+
78+
* Time complexity : O(n).
79+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# 2790. Maximum Number of Groups With Increasing Length
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Math, Binary Search, Greedy, Sorting.
5+
- Similar Questions: Group the People Given the Group Size They Belong To.
6+
7+
## Problem
8+
9+
You are given a **0-indexed** array `usageLimits` of length `n`.
10+
11+
Your task is to create **groups** using numbers from `0` to `n - 1`, ensuring that each number, `i`, is used no more than `usageLimits[i]` times in total **across all groups**. You must also satisfy the following conditions:
12+
13+
14+
15+
- Each group must consist of **distinct **numbers, meaning that no duplicate numbers are allowed within a single group.
16+
17+
- Each group (except the first one) must have a length **strictly greater** than the previous group.
18+
19+
20+
Return **an integer denoting the **maximum** number of groups you can create while satisfying these conditions.**
21+
22+
 
23+
Example 1:
24+
25+
```
26+
Input: usageLimits = [1,2,5]
27+
Output: 3
28+
Explanation: In this example, we can use 0 at most once, 1 at most twice, and 2 at most five times.
29+
One way of creating the maximum number of groups while satisfying the conditions is:
30+
Group 1 contains the number [2].
31+
Group 2 contains the numbers [1,2].
32+
Group 3 contains the numbers [0,1,2].
33+
It can be shown that the maximum number of groups is 3.
34+
So, the output is 3.
35+
```
36+
37+
Example 2:
38+
39+
```
40+
Input: usageLimits = [2,1,2]
41+
Output: 2
42+
Explanation: In this example, we can use 0 at most twice, 1 at most once, and 2 at most twice.
43+
One way of creating the maximum number of groups while satisfying the conditions is:
44+
Group 1 contains the number [0].
45+
Group 2 contains the numbers [1,2].
46+
It can be shown that the maximum number of groups is 2.
47+
So, the output is 2.
48+
```
49+
50+
Example 3:
51+
52+
```
53+
Input: usageLimits = [1,1]
54+
Output: 1
55+
Explanation: In this example, we can use both 0 and 1 at most once.
56+
One way of creating the maximum number of groups while satisfying the conditions is:
57+
Group 1 contains the number [0].
58+
It can be shown that the maximum number of groups is 1.
59+
So, the output is 1.
60+
```
61+
62+
 
63+
**Constraints:**
64+
65+
66+
67+
- `1 <= usageLimits.length <= 105`
68+
69+
- `1 <= usageLimits[i] <= 109`
70+
71+
72+
73+
## Solution
74+
75+
```javascript
76+
/**
77+
* @param {number[]} usageLimits
78+
* @return {number}
79+
*/
80+
var maxIncreasingGroups = function(usageLimits) {
81+
var count = 0;
82+
var num = 0;
83+
var minCount = 1;
84+
usageLimits.sort((a, b) => a - b);
85+
for (var i = 0; i < usageLimits.length; i++) {
86+
count += usageLimits[i];
87+
if (count >= minCount) {
88+
num++;
89+
minCount += num + 1;
90+
}
91+
}
92+
return num;
93+
};
94+
```
95+
96+
**Explain:**
97+
98+
Math.
99+
100+
**Complexity:**
101+
102+
* Time complexity : O(n).
103+
* Space complexity : O(1).

0 commit comments

Comments
 (0)