当前位置:网站首页>LeetCode 1403 非递增顺序的最小子序列[贪心] HERODING的LeetCode之路
LeetCode 1403 非递增顺序的最小子序列[贪心] HERODING的LeetCode之路
2022-08-04 12:54:00 【HERODING23】

解题思路:
首先获取数组的和,然后对数组进行排序,由于尽可能找长度最小和大于原数组和一半的序列,所以从大往小找即可,代码如下:
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;
}
};
边栏推荐
- ShanDong Multi-University Training #4 A、B、C、G
- 【Game Of AutoTest】1、再度启程,重识游戏自动化测试
- 集群监控——Zabbix
- Django使用腾讯云发送短信并存入redis
- Django框架MySQL数据库到models模型的映射关系
- A Collection of Flutter Tutorials (2022 Edition)
- 【自动微分实现】反向OO实现自动微分(Pytroch核心机制)
- 程序猿七夕礼物-如何30分钟给女朋友快速搭建专属语聊房
- MySQL-数据类型
- Why is Luo Zhenyu's A-share dream so difficult to fulfill?
猜你喜欢

手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果

视觉SLAM十四讲学习笔记 第7讲 视觉里程计

判断密码是否包含键盘连续字母

【黑马早报】尚乘数科上市13天,市值超阿里;北大终止陈春花聘用合同;新东方花近200亿退学费和遣散费;张小泉75%产品贴牌代工...

Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes

SCA兼容性分析工具(ORACLE/MySQL/DB2---&gt;MogDB/openGauss/PostgreSQL)

leetcode 48. Rotate Image 旋转图像(Medium)

redis未授权访问漏洞【vulhub靶场】复现

双目立体视觉学习笔记(一)

MFC的相机双目标定界面设计
随机推荐
【VSCode】一文详解vscode下安装vim后无法使用Ctrl+CV复制粘贴 使用Vim插件的配置记录
【UML】信息系统分析与设计知识点总结
Analysis and comparison of mobile cross-end technical solutions
【解决方案 三十一】Navicat数据库结构同步
Haproxy搭建web群集
跨链桥已成行业最大安全隐患 为什么和怎么办
Why is Luo Zhenyu's A-share dream so difficult to fulfill?
Two years of independent development experience Programmers tell us the experience of making money (listen to the masters who really make money)
双目立体视觉笔记(二)
postgre 支持 newsql 特性可行性有多大?
String is a reference type
Geoffrey Hinton:深度学习的下一个大事件
持续交付(四)Jenkins多线程任务执行
ShanDong Multi-University Training #4 A、B、C、G
Js获取当前页面url参数
绩效考核带给员工的不能只是压力
《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
Systemui qsSetting添加新图标
"Lonely Walking on the Moon" is a powerful medicine, it can't cure the internal friction of happy twist
小程序对接企业微信客服