2358. Maximum Number of Groups Entering a Competition LeetCode Solution
In this guide, you will get 2358. Maximum Number of Groups Entering a Competition LeetCode Solution with the best time and space complexity. The solution to Maximum Number of Groups Entering a Competition problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Number of Groups Entering a Competition solution in C++
- Maximum Number of Groups Entering a Competition solution in Java
- Maximum Number of Groups Entering a Competition solution in Python
- Additional Resources
Problem Statement of Maximum Number of Groups Entering a Competition
You are given a positive integer array grades which represents the grades of students in a university. You would like to enter all these students into a competition in ordered non-empty groups, such that the ordering meets the following conditions:
The sum of the grades of students in the ith group is less than the sum of the grades of students in the (i + 1)th group, for all groups (except the last).
The total number of students in the ith group is less than the total number of students in the (i + 1)th group, for all groups (except the last).
Return the maximum number of groups that can be formed.
Example 1:
Input: grades = [10,6,12,7,3,5]
Output: 3
Explanation: The following is a possible way to form 3 groups of students:
– 1st group has the students with grades = [12]. Sum of grades: 12. Student count: 1
– 2nd group has the students with grades = [6,7]. Sum of grades: 6 + 7 = 13. Student count: 2
– 3rd group has the students with grades = [10,3,5]. Sum of grades: 10 + 3 + 5 = 18. Student count: 3
It can be shown that it is not possible to form more than 3 groups.
Example 2:
Input: grades = [8,8]
Output: 1
Explanation: We can only form 1 group, since forming 2 groups would lead to an equal number of students in both groups.
Constraints:
1 <= grades.length <= 105
1 <= grades[i] <= 105
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
2358. Maximum Number of Groups Entering a Competition LeetCode Solution in C++
class Solution {
public:
int maximumGroups(vector<int>& grades) {
// Sort grades, then we can seperate the students into groups of sizes 1, 2,
// 3, ..., k, s.t. the i-th group < the (i + 1)-th group for both sum and
// size. So, we can rephrase the problem into:
// Find the maximum k s.t. 1 + 2 + 3 + ... + k <= n
// 1 + 2 + 3 + ... + k <= n
// k(k + 1) / 2 <= n
// k^2 + k <= 2n
// (k + 0.5)^2 - 0.25 <= 2n
// (k + 0.5)^2 <= 2n + 0.25
// k <= sqrt(2n + 0.25) - 0.5
return sqrt(grades.size() * 2 + 0.25) - 0.5;
}
};
/* code provided by PROGIEZ */
2358. Maximum Number of Groups Entering a Competition LeetCode Solution in Java
class Solution {
public int maximumGroups(int[] grades) {
// Sort grades, then we can seperate the students into groups of sizes 1, 2,
// 3, ..., k, s.t. the i-th group < the (i + 1)-th group for both sum and
// size. So, we can rephrase the problem into:
// Find the maximum k s.t. 1 + 2 + 3 + ... + k <= n
// 1 + 2 + 3 + ... + k <= n
// k(k + 1) / 2 <= n
// k^2 + k <= 2n
// (k + 0.5)^2 - 0.25 <= 2n
// (k + 0.5)^2 <= 2n + 0.25
// k <= sqrt(2n + 0.25) - 0.5
return (int) (Math.sqrt(grades.length * 2 + 0.25) - 0.5);
}
}
// code provided by PROGIEZ
2358. Maximum Number of Groups Entering a Competition LeetCode Solution in Python
class Solution:
def maximumGroups(self, grades: list[int]) -> int:
# Sort grades, then we can seperate the students into groups of sizes 1, 2,
# 3, ..., k, s.t. the i-th group < the (i + 1)-th group for both sum and
# size. So, we can rephrase the problem into:
# Find the maximum k s.t. 1 + 2 + 3 + ... + k <= n
# 1 + 2 + 3 + ... + k <= n
# k(k + 1) // 2 <= n
# k^2 + k <= 2n
# (k + 0.5)^2 - 0.25 <= 2n
# (k + 0.5)^2 <= 2n + 0.25
# k <= sqrt(2n + 0.25) - 0.5
return int(math.sqrt(len(grades) * 2 + 0.25) - 0.5)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.