Skip to content

Commit 0bc700f

Browse files
authored
Merge pull request #2012 from vivekvardhan2810/main
Maximum Number of Groups With Increasing Length (Leetcode) Added problem number 2790
2 parents 16cfcf8 + f315792 commit 0bc700f

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
id: maximum-number-of-groups-with-increasing-length
3+
title: Maximum Number of Groups With Increasing Length
4+
sidebar_label: 2790 Maximum Number of Groups With Increasing Length
5+
tags:
6+
- Java
7+
- Math
8+
- Array
9+
- Binary Search
10+
- Greedy
11+
- Sorting
12+
description: "This document provides a solution where we Return an integer denoting the maximum number of groups you can create while satisfying these conditions."
13+
---
14+
15+
## Problem
16+
17+
You are given a **0-indexed** array $usageLimits$ of length $n$.
18+
19+
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:
20+
21+
- Each group must consist of **distinct** numbers, meaning that no duplicate numbers are allowed within a single group.
22+
23+
- Each group (except the first one) must have a length **strictly greater** than the previous group.
24+
25+
Return an integer denoting the **maximum** number of groups you can create while satisfying these conditions.
26+
27+
### Examples
28+
29+
**Example 1:**
30+
31+
```
32+
Input: usageLimits = [1,2,5]
33+
34+
Output: 3
35+
36+
Explanation: In this example, we can use 0 at most once, 1 at most twice, and 2 at most five times.
37+
38+
One way of creating the maximum number of groups while satisfying the conditions is:
39+
40+
Group 1 contains the number [2].
41+
42+
Group 2 contains the numbers [1,2].
43+
44+
Group 3 contains the numbers [0,1,2].
45+
46+
It can be shown that the maximum number of groups is 3.
47+
48+
So, the output is 3.
49+
50+
```
51+
**Example 2:**
52+
53+
```
54+
Input: usageLimits = [2,1,2]
55+
56+
Output: 2
57+
58+
Explanation: In this example, we can use 0 at most twice, 1 at most once, and 2 at most twice.
59+
60+
One way of creating the maximum number of groups while satisfying the conditions is:
61+
62+
Group 1 contains the number [0].
63+
64+
Group 2 contains the numbers [1,2].
65+
66+
It can be shown that the maximum number of groups is 2.
67+
68+
So, the output is 2.
69+
70+
```
71+
72+
**Example 3:**
73+
74+
```
75+
Input: usageLimits = [1,1]
76+
77+
Output: 1
78+
79+
Explanation: In this example, we can use both 0 and 1 at most once.
80+
81+
One way of creating the maximum number of groups while satisfying the conditions is:
82+
83+
Group 1 contains the number [0].
84+
85+
It can be shown that the maximum number of groups is 1.
86+
87+
So, the output is 1.
88+
```
89+
90+
### Constraints
91+
92+
- $1 <= usageLimits.length <= 10^5$
93+
- $1 <= usageLimits[i] <= 10^9$
94+
---
95+
96+
## Approach
97+
98+
To solve the problem, we need to understand the nature of the allowed moves:
99+
100+
1. **Sort the Usage Limits**:
101+
102+
- Start by sorting the **'usageLimits'** array. This helps in efficiently forming groups by using the smallest available numbers first, which maximizes the number of groups we can form.
103+
104+
2. **Initialize Variables**:
105+
106+
- **'groups'**: Keeps track of the number of groups formed.
107+
108+
- **'currentGroupSize'**: Represents the required size for the next group to be formed, starting at 1.
109+
110+
- **'availableNumbers'**: Tracks the total available slots from all numbers seen so far.
111+
112+
3. **Form Groups**:
113+
114+
- Iterate through the sorted **'usageLimits'**.
115+
116+
- For each limit, add it to **'availableNumbers'**.
117+
118+
- Check if **'availableNumbers'** is at least as large as **'currentGroupSize'**.
119+
120+
- If true, it means we can form a new group of size **'currentGroupSize'**.
121+
122+
- Increment the **'groups'** counter, decrement **'availableNumbers'** by **'currentGroupSize'**, and **'increase'** currentGroupSize by 1 for the next group.
123+
124+
## Solution for Maximum Number of Groups With Increasing Length
125+
126+
- The core idea is to maximize the number of groups by ensuring each group has a distinct set of numbers and each subsequent group is larger than the previous one.
127+
128+
- By sorting the **'usageLimits'**, we can efficiently use the smallest available counts first to form valid groups.
129+
130+
- This approach ensures that we can form as many groups as possible without exceeding the usage limits.
131+
132+
#### Code in Java
133+
134+
```java
135+
import java.util.Collections;
136+
import java.util.List;
137+
138+
class Solution {
139+
public int maxIncreasingGroups(List<Integer> usageLimits) {
140+
// Sort the usageLimits in ascending order
141+
Collections.sort(usageLimits);
142+
143+
// Initialize variables
144+
int groups = 0;
145+
int currentGroupSize = 1; // the size of the next group we want to form
146+
long availableNumbers = 0; // to track the number of available slots across all numbers
147+
148+
// Traverse the sorted usageLimits
149+
for (int limit : usageLimits) {
150+
availableNumbers += limit; // add the current limit to available slots
151+
152+
// Check if we can form the next group
153+
if (availableNumbers >= currentGroupSize) {
154+
groups++; // form a new group
155+
availableNumbers -= currentGroupSize; // reduce the used slots
156+
currentGroupSize++; // increase the size requirement for the next group
157+
}
158+
}
159+
160+
return groups;
161+
}
162+
}
163+
```
164+
165+
### Complexity Analysis
166+
167+
#### Time Complexity: $O(nlogn)$
168+
169+
> **Reason**: Time Complexity is $O(nlogn)$. Sorting the **'usageLimits'** takes $O(nlogn)$ time. Iterating through the sorted list takes $O(n)$ time. Therefore, the overall time complexity is $O(nlogn)$.
170+
171+
#### Space Complexity: $O(1)$
172+
173+
> **Reason**: The space complexity is $O(1)$, Because the additional space (ignoring the space used by the input list and the space needed for sorting, which is $O(n)$).
174+
175+
# References
176+
177+
- **LeetCode Problem:** [Maximum Number of Groups With Increasing Length](https://leetcode.com/problems/maximum-number-of-groups-with-increasing-length/description/)
178+
- **Solution Link:** [Maximum Number of Groups With Increasing Length Solution on LeetCode](https://leetcode.com/problems/maximum-number-of-groups-with-increasing-length/solutions/)
179+
- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/vivekvardhan43862/)

0 commit comments

Comments
 (0)