[LeetCode] Maximum Number of Groups Entering a Competition

2358. 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int maximumGroups(vector<int>& A) {
sort(begin(A), end(A));
int sum = 0, sz = 0, res = 0, n = A.size(), now = 0, nowsz = 0;
for(int i = 0; i < n; i++) {
now += A[i];
nowsz++;
if(now > sum and nowsz > sz) {
sum = now;
sz = nowsz;
now = nowsz = 0;
res++;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/31/PS/LeetCode/maximum-number-of-groups-entering-a-competition/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.