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

Motion Rule (16)-Union Check Basic Questions-Grid Game

牛客网刷题记录 || 链表

【UML】信息系统分析与设计知识点总结

Haproxy搭建web群集

罗振宇的A股梦,咋这么难圆?

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

鲜花“刺客”收割七夕

PMP每日一练 | 考试不迷路-8.4(包含敏捷+多选)

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

Interviewer: How to view files containing abc string in /etc directory?
随机推荐
nVisual二次开发——第二章 nVisual API操作指南Swagger使用
【软考 系统架构设计师】软件架构设计② 软件架构风格
[UML] Summary of Information System Analysis and Design Knowledge Points
Django框架MySQL数据库到models模型的映射关系
从零开始配置 vim(6)——缩写
17种正则表达式
JSX使用
ES 节点2G内存分析
Why don't young people like to buy Mengniu and Yili?
redisTemplate存取List遇到的坑
Do you understand the various configurations in the project?
正确使用Impala的invalidate metadata与refresh语句
【自动微分实现】反向OO实现自动微分(Pytroch核心机制)
鲜花“刺客”收割七夕
面试官:说一下NIO和BIO的区别
LeetCode_424_替换后的最长重复字符
Script to get local IP address
视觉SLAM十四讲学习笔记 第7讲 视觉里程计
“蔚来杯“2022牛客暑期多校训练营5 B、C、F、G、H、K
MySQL-数据类型