当前位置:网站首页>leetcode-6133: maximum number of groupings
leetcode-6133: maximum number of groupings
2022-08-01 07:58:00 【chrysanthemum bat】
题目
给你一个正整数数组 grades ,Represents the grades of some students in the university.你打算将 所有 Students are divided into groups 有序 non-empty grouping of,The order among the groups satisfies all of the following conditions:
第 i The total score of students in each group 小于 第 (i + 1) The total score of students in each group,true for all groups(Except for the last group).
第 i The total number of students in a group 小于 第 (i + 1) The total number of students in a group,true for all groups(Except for the last group).
Returns formable 最大 组数.
示例 1:
输入:grades = [10,6,12,7,3,5]
输出:3
解释:Below is the formation 3 A possible way of grouping:
- 第 1 The grades of students in each group are grades = [12] ,总成绩:12 ,学生数:1
- 第 2 The grades of students in each group are grades = [6,7] ,总成绩:6 + 7 = 13 ,学生数:2
- 第 3 The grades of students in each group are grades = [10,3,5] ,总成绩:10 + 3 + 5 = 18 ,学生数:3
It can be shown that it cannot be formed over 3 个分组.
示例 2:
输入:grades = [8,8]
输出:1
解释:只能形成 1 个分组,Because if to form 2 a group,would result in an equal number of students in each group.
解题
方法一:脑筋急转弯
如果从小到大排序
那么每次取 1个、2个、3个… Get the maximum amount you can get
而根grades具体是什么值,没有任何关系.
因此假设grades的值,是1、2、3、4、5、6、7…
第二次取2个元素,The resulting grouping sum must be greater than the first one taken1个元素
nis the number of all elements
Calculate with arithmetic progression (首项+末项)*项数/2<=n
Calculate the maximum number of groupsx即可
class Solution {
public:
int maximumGroups(vector<int>& grades) {
int len=grades.size();
return sqrt(2*len+0.25)-0.5;
}
};
方法二:排序+贪心
从小到大排序
取1个、2个、3个…以此类推
Calculate the maximum number of groups that can be obtained
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;
}
};
边栏推荐
- 【STM32】入门(一):环境搭建、编译、下载、运行
- 升级为重量级锁,锁重入会导致锁释放?
- 13 - JUC CountDownLatch concurrent programming
- pytest interface automation testing framework | parametrize source code analysis
- 巧妙利用unbuffer实时写入
- LevelSequence源码分析
- How to use Photoshop to composite star trail photos, post-processing method of night sky star trail photos
- 搜索框字符自动补全
- 自制一款远程控制软件——VeryControl
- 类似 MS Project 的项目管理工具有哪些
猜你喜欢
随机推荐
nodetype中值1、2、3分别代表什么意思
Vim三种模式
Summary of test points about app updates in different ways
13 - JUC CountDownLatch concurrent programming
基于tika实现对文件类型进行判断
What do the values 1, 2, and 3 in nodetype mean?
pytest接口自动化测试框架 | 执行失败跳转pdb
GO error handling
Pod环境变量和initContainer
zip package all files in the directory (including hidden files/folders)
Electromagnetic compatibility introductory tutorial (6) test project
pytest接口自动化测试框架 | 跳过测试类
Delphi MDI appliction documents maximize display, remove buttons such as maximize and minimize
我的创作纪念日
Golang:go获取url和表单属性值
Centos install php7.4, build hyperf, forward RDS
VoLTE基础学习系列 | 企业语音网简述
【一句话攻略】彻底理解JS中的回调(Callback)函数
How to generate and configure public key certificate in Alipay
LevelSequence源码分析









