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

03 多线程与高并发 - ReentrantLock 源码解析

CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔,可用于改善循环时间

【自动微分实现】反向OO实现自动微分(Pytroch核心机制)

RobotFramework二次开发(一)

一文梳理NLP主要模型发展脉络

Hit the interview!The latest interview booklet of Ali Jin, nine silver and ten is stable!

Chinese valentine's day of young people crazy to make money, earn 140000 a week

【PHP实现微信公众平台开发—基础篇】第2章 微信公众账号及申请流程详解

技术分享| 小程序实现音视频通话

“蔚来杯“2022牛客暑期多校训练营5 B、C、F、G、H、K
随机推荐
绩效考核带给员工的不能只是压力
持续交付(三)Jenkinsfile语法使用介绍
“蔚来杯“2022牛客暑期多校训练营3 C
备份控制文件
技术分享| 小程序实现音视频通话
Motion Rule (16)-Union Check Basic Questions-Relations
router---路由守卫
[UML] Summary of Information System Analysis and Design Knowledge Points
【微信小程序】信息管理与信息系统专业社会实习制作项目--垃圾指纹
《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
年轻人为什么不喜欢买蒙牛、伊利了?
du命令_set命令选项
永磁同步电机FOC驱动代码讲解
【PHP实现微信公众平台开发—基础篇】第2章 微信公众账号及申请流程详解
js正则表达式提取内容
双目立体视觉笔记(三)三角测量、极线校正
持续交付(二)PipeLine基本使用
七夕疯狂搞钱的年轻人,一周赚14万
Haproxy搭建web群集
Cows 树状数组