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

The log causes these pits in the thread block, you have to prevent

特殊的日子,值得纪念

VSCode插件推荐(Rust环境)

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

自制一款远程控制软件——VeryControl

Data Analysis 6

2022杭电中超杯(1)个人题解

Self-made a remote control software - VeryControl

【HDLBits 刷题】Circuits(1)Combinational Logic

聊一聊ICMP协议以及ping的过程
随机推荐
【HDLBits 刷题】Circuits(1)Combinational Logic
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
网络基础学习
C语言学习概览(一)
How to generate and configure public key certificate in Alipay
微信小程序请求封装
SaaS安全认证综合指南
2022.7.31-----leetcode.1161
flink sql-client,怎么处理源端与目标增加端,sql-client包括映射表与JOB如
USB Protocol (2) Terminology
mysql查看cpu使用情况
13 - JUC CountDownLatch concurrent programming
22 Niu Ke Duo School 1 I. Chiitoitsu (Probability dp)
HoloView--Customization
leetcode-6135:图中的最长环
USB 协议 (二) 术语
云原生FAQ
支付宝如何生成及配置公钥证书
rhcsa 第四天
VSCode插件推荐(Rust环境)