当前位置:网站首页>leetcode-6133:分组的最大数量
leetcode-6133:分组的最大数量
2022-08-01 07:50:00 【菊头蝙蝠】
题目
给你一个正整数数组 grades ,表示大学中一些学生的成绩。你打算将 所有 学生分为一些 有序 的非空分组,其中分组间的顺序满足以下全部条件:
第 i 个分组中的学生总成绩 小于 第 (i + 1) 个分组中的学生总成绩,对所有组均成立(除了最后一组)。
第 i 个分组中的学生总数 小于 第 (i + 1) 个分组中的学生总数,对所有组均成立(除了最后一组)。
返回可以形成的 最大 组数。
示例 1:
输入:grades = [10,6,12,7,3,5]
输出:3
解释:下面是形成 3 个分组的一种可行方法:
- 第 1 个分组的学生成绩为 grades = [12] ,总成绩:12 ,学生数:1
- 第 2 个分组的学生成绩为 grades = [6,7] ,总成绩:6 + 7 = 13 ,学生数:2
- 第 3 个分组的学生成绩为 grades = [10,3,5] ,总成绩:10 + 3 + 5 = 18 ,学生数:3
可以证明无法形成超过 3 个分组。
示例 2:
输入:grades = [8,8]
输出:1
解释:只能形成 1 个分组,因为如果要形成 2 个分组的话,会导致每个分组中的学生数目相等。
解题
方法一:脑筋急转弯
如果从小到大排序
那么每次取 1个、2个、3个… 就能得到最大的能取得的数量
而根grades具体是什么值,没有任何关系。
因此假设grades的值,是1、2、3、4、5、6、7…
第二次取2个元素,得到的分组和一定是大于第一次取的1个元素
n为所有元素个数
用等差数列计算 (首项+末项)*项数/2<=n
计算得到最大组数x即可
class Solution {
public:
int maximumGroups(vector<int>& grades) {
int len=grades.size();
return sqrt(2*len+0.25)-0.5;
}
};
方法二:排序+贪心
从小到大排序
取1个、2个、3个…以此类推
计算能取得最大组数
class Solution {
public:
int maximumGroups(vector<int>& grades) {
sort(grades.begin(),grades.end());
int res=1,n=grades.size();
int preVal=grades[0];
int preCount=1;
int curVal=0;
int curCount=0;
int i=1;
while(i<n){
while(true){
if(curCount>preCount&&curVal>preVal) break;
if(i==n) return res;
curVal+=grades[i];
curCount++;
i++;
}
res++;
preVal=curVal;
preCount=curCount;
curVal=0;
curCount=0;
}
return res;
}
};
边栏推荐
- pytest interface automation testing framework | single/multiple parameters
- Pytest | skip module interface test automation framework
- Golang: go get url and form attribute value
- How to generate and configure public key certificate in Alipay
- 云原生FAQ
- Flink SQL - client, how to deal with the source side and to increase the target, the SQL - client including mapping table and the JOB such as
- I have three degrees, and I have five faces. I was "confessed" by the interviewer, and I got an offer of 33*15.
- R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的pad_fn函数与gt::fmt函数一起用于填充包含数值的特定列、对数据列的数值进行十进制对齐(从小数点对齐)
- 电磁兼容简明教程(6)测试项目
- 【杭电多校第四场 B题】最短路图+缩点dp
猜你喜欢
日志导致线程Block的这些坑,你不得不防
22牛客多校1 C.Grab the Seat (几何 + 暴力)
走进音视频的世界——mp3封装格式
Upgrade to heavyweight lock, lock reentrancy will lead to lock release?
微信小程序请求封装
Golang: go open web service
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
VSCode插件推荐(Rust环境)
七夕来袭——属于程序员的浪漫
How to generate and configure public key certificate in Alipay
随机推荐
关于App不同方式更新的测试点归纳
升级为重量级锁,锁重入会导致锁释放?
【杭电多校第四场 B题】最短路图+缩点dp
POJ2031空间站题解
日志导致线程Block的这些坑,你不得不防
扁平数组转树结构实现方式
【MySQL】操作表DML相关语句
22牛客多校1 I. Chiitoitsu (概率dp)
最小生成树
Generate pictures based on the content of the specified area and share them with a summary
Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
Centos install php7.4, build hyperf, forward RDS
zip package all files in the directory (including hidden files/folders)
好的plm软件有哪些?plm软件排行榜
2022.7.31-----leetcode.1161
372. 超级次方
GO error handling
国内外最顶级的8大plm项目管理系统
pytest接口自动化测试框架 | parametrize中ids的用法
类似 MS Project 的项目管理工具有哪些