当前位置:网站首页>LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road
LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road
2022-08-04 13:03:00 【HERODING23】
解题思路:
First get the sum of the array,然后对数组进行排序,Because as far as possible to find the sequence with the smallest length and greater than half of the original array,So start from big to small,代码如下:
class Solution {
public:
vector<int> minSubsequence(vector<int>& nums) {
int n = nums.size();
int total = accumulate(nums.begin(), nums.end(), 0);
sort(nums.begin(), nums.end());
vector<int> ans;
int cur = 0;
for(int i = n - 1; i >= 0; i --) {
if(cur <= total / 2) {
ans.emplace_back(nums[i]);
cur += nums[i];
} else {
break;
}
}
return ans;
}
};
边栏推荐
猜你喜欢
CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔,可用于改善循环时间
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
一文梳理NLP主要模型发展脉络
Access Huawei game anti-addiction, click the anti-addiction pop-up window, the game crashes
MFC的相机双目标定界面设计
Cool and efficient data visualization big screen, it's really not that difficult to do!丨Geek Planet
【UML】信息系统分析与设计知识点总结
Haproxy搭建web群集
03 多线程与高并发 - ReentrantLock 源码解析
面试官:说一下NIO和BIO的区别
随机推荐
router---Programmatic navigation
MogDB/openGauss 3.0 扩容及缩容
Diffusion Models:生成扩散模型
汉诺塔怎么玩
从零开始配置 vim(6)——缩写
Django使用腾讯云发送短信并存入redis
Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes
Launcher app prediction
sqlserver删除重复数据
技术分享| 小程序实现音视频通话
为什么密码云服务平台是云时代的必然之选?
odoo13笔记点
String is a reference type
Chinese valentine's day of young people crazy to make money, earn 140000 a week
JSX使用
一文梳理NLP主要模型发展脉络
Django框架MySQL数据库到models模型的映射关系
The head module of the yolo series
PMP每日一练 | 考试不迷路-8.4(包含敏捷+多选)
A Collection of Flutter Tutorials (2022 Edition)