当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
情人节浪漫3D照片墙【附源码】
break与continue超详解!!!
Hit the interview!The latest interview booklet of Ali Jin, nine silver and ten is stable!
年轻人为什么不喜欢买蒙牛、伊利了?
Do you understand the various configurations in the project?
RK1126编译gdb 板子上gdb调试程序
A comprehensive understanding of MOS tubes, an article is enough
使用COLMAP初步三维重建
Why don't young people like to buy Mengniu and Yili?
随机推荐
ROS设置plugin插件
Cool and efficient data visualization big screen, it's really not that difficult to do!丨Geek Planet
A comprehensive understanding of MOS tubes, an article is enough
RK1126编译gdb 板子上gdb调试程序
MFC的相机双目标定界面设计
SCA兼容性分析工具(ORACLE/MySQL/DB2---&gt;MogDB/openGauss/PostgreSQL)
为什么密码云服务平台是云时代的必然之选?
HDU1580 输出先手能取的方案数
Motion Rule (16)-Union Check Basic Questions-Grid Game
LeetCode_299_猜数字游戏
论文翻译:2022_Time-Frequency Attention for Monaural Speech Enhancement
双目立体视觉笔记(二)
8/3 训练日志 (树状数组+区间覆盖+思维+01字典树)
他是“中台”之父,凭一个概念为阿里狂赚百亿
d不要直接用转串
LeetCode_643_子数组的最大平均数Ⅰ
leetcode 48. Rotate Image 旋转图像(Medium)
搭建ros交叉编译环境(从x86到nvidia arm)
使用SQLServer复制数据库
未来已来,只是尚未流行