Skip to content

Commit bfd6fcd

Browse files
committed
feat: solve No.2790
1 parent d9fed52 commit bfd6fcd

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
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)