当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
Hit the interview!The latest interview booklet of Ali Jin, nine silver and ten is stable!
一文梳理NLP主要模型发展脉络
The head module of the yolo series
到底什么是真正的HTAP?
Arduino框架下I2S控制ADC采样以及PWM输出示例解析
MySQL-数据类型
redis未授权访问漏洞【vulhub靶场】复现
MATLAB——图像分块
Chinese valentine's day of young people crazy to make money, earn 140000 a week
倒计时 3 天|一起看云原生 Meetup 的六大议题
随机推荐
面试官:如何查看/etc目录下包含abc字符串的文件?
MySQL-数据类型
d不要直接用转串
A Collection of Flutter Tutorials (2022 Edition)
持续交付(四)Jenkins多线程任务执行
SSRF-服务器端请求伪造-相关知识
FHQ-Treap 简介
内存定位利器-ASAN使用小结
Django框架MySQL数据库到models模型的映射关系
Valentine's Day Romantic 3D Photo Wall [with source code]
LeetCode_643_子数组的最大平均数Ⅰ
Oracle 19c 单实例 19.3.0 升级到19.11.0 详细教程
21天学习挑战赛--第二天打卡(setSystemUiVisibility、导航栏、状态栏)
从零开始配置 vim(6)——缩写
8/3 训练日志 (树状数组+区间覆盖+思维+01字典树)
PMP每日一练 | 考试不迷路-8.4(包含敏捷+多选)
视觉SLAM十四讲学习笔记 第7讲 视觉里程计
ShanDong Multi-University Training #4 A、B、C、G
【Game Of AutoTest】1、再度启程,重识游戏自动化测试
绩效考核带给员工的不能只是压力