#2358

Maximum Number of Groups Entering a Competition

pupil · 595 · lc medium +29 · verified · 68.5% accepted · 713 likes · top 76%

Description

You have a positive integer array grades representing student grades. Assign all students to ordered non-empty groups satisfying:

- Each group's total grade exceeds the previous group's total grade.

- Each group's student count exceeds the previous group's student count.

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.

Code

1
2
3