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

倒计时 3 天|一起看云原生 Meetup 的六大议题

并发刺客(False Sharing)——并发程序的隐藏杀手

基于双层共识控制的直流微电网优化调度(Matlab代码实现)

未来已来,只是尚未流行

MFC的相机双目标定界面设计

七夕疯狂搞钱的年轻人,一周赚14万

Interviewer: Tell me the difference between NIO and BIO

"Lonely Walking on the Moon" is a powerful medicine, it can't cure the internal friction of happy twist

持续交付(二)PipeLine基本使用

Ceres库运行,模板内报内存冲突问题。(已解决)
随机推荐
漏洞复现 - - - Alibaba Nacos权限认证绕过
【软考 系统架构设计师】软件架构设计② 软件架构风格
LeetCode_299_猜数字游戏
String is a reference type
《独行月球》猛药,治不了开心麻花内耗
密码设置有关方法:不能相同字母,不能为连续字符
oracle sql中根据条件判断是否插入数据
ES 节点2G内存分析
高手,云集在于REST、gRPC 和 GraphQL之间!
并发刺客(False Sharing)——并发程序的隐藏杀手
sqlplus报错ORA-12547: TNS:lost contact解决
使用COLMAP初步三维重建
【WeChat Mini Program】Social Internship Production Project for Information Management and Information System Major--Trash Fingerprint
Django使用腾讯云发送短信并存入redis
用过Apifox这个API接口工具后,确实感觉postman有点鸡肋......
“蔚来杯“2022牛客暑期多校训练营3 C
Ceres库运行,模板内报内存冲突问题。(已解决)
双目立体视觉笔记(二)
Ultra-QuickSort
备份控制文件