当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

Self-made a remote control software - VeryControl

Chapter 9 of Huawei Deep Learning Course - Convolutional Neural Network and Case Practice

Golang:go获取url和表单属性值

如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法

热修复技术可谓是百花齐放

LabVIEW RT中的用户界面更新速度

配置我的kitty

Vim扩展内容

Microsoft Azure & NVIDIA IoT 开发者季 I|Azure IoT & NVIDIA Jetson 开发基础

SAP ABAP ALV+SMARTFORS 表分页 报表打印程序
随机推荐
zip打包目录所有文件(含隐藏文件/夹)
HoloView--live data
app 自动化 通过工具查看app 元素 (三)
将aof文件转换为命令waoffle安装和使用
sqlserver 对比两张表的差异
app 自动化 打开app (二)
Redis 3.2.3 crashed by signal: 11 服务宕机问题排查
Shell executes SQL to send emails
13 - JUC CountDownLatch concurrent programming
Golang:go连接和使用mysql
并发编程13-JUC之CountDownLatch
Vim三种模式
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
VSCode 快捷键及通用插件推荐
22牛客多校1 C.Grab the Seat (几何 + 暴力)
Golang:go模版引擎的使用
静态Pod、Pod创建流程、容器资源限制
Delphi MDI appliction documents maximize display, remove buttons such as maximize and minimize
my creative day
Golang: go static file processing