当前位置:网站首页>【LeetCode】1403.非递增顺序的最小子序列
【LeetCode】1403.非递增顺序的最小子序列
2022-08-04 10:50:00 【酥酥~】
题目
给你一个数组 nums,请你从中抽取一个子序列,满足该子序列的元素之和 严格 大于未包含在该子序列中的各元素之和。
如果存在多个解决方案,只需返回 长度最小 的子序列。如果仍然有多个解决方案,则返回 元素之和最大 的子序列。
与子数组不同的地方在于,「数组的子序列」不强调元素在原数组中的连续性,也就是说,它可以通过从数组中分离一些(也可能不分离)元素得到。
注意,题目数据保证满足所有约束条件的解决方案是 唯一 的。同时,返回的答案应当按 非递增顺序 排列。
示例 1:
输入:nums = [4,3,10,9,8]
输出:[10,9]
解释:子序列 [10,9] 和 [10,8] 是最小的、满足元素之和大于其他各元素之和的子序列。但是 [10,9] 的元素之和最大。
示例 2:
输入:nums = [4,4,7,6,7]
输出:[7,7,6]
解释:子序列 [7,7] 的和为 14 ,不严格大于剩下的其他元素之和(14 = 4 + 4 + 6)。因此,[7,6,7] 是满足题意的最小子序列。注意,元素按非递增顺序返回。
示例 3:
输入:nums = [6]
输出:[6]
提示:
1 <= nums.length <= 500
1 <= nums[i] <= 100
题解
贪心
从大到小依次取出,直到取出的子序列和严格大于剩余元素和
class Solution {
public:
vector<int> minSubsequence(vector<int>& nums) {
sort(nums.begin(),nums.end(),greater<int>());
int sum = accumulate(nums.begin(),nums.end(),0);
vector<int> result;
int sum_tmp = 0;
for(int k:nums)
{
result.push_back(k);
sum_tmp+=k;
if(sum_tmp > sum - sum_tmp)
break;
}
return result;
}
};
边栏推荐
- Using .NET to simply implement a high-performance clone of Redis (2)
- Super Learning Method
- iMeta | 百度认证完成,搜索“iMeta”直达出版社主页和投稿链接
- Jina 实例秀|七夕神器!比你更懂你女友的AI口红推荐
- MySQL: Integrity Constraints and Table Design Principles
- Advanced transcriptome analysis and R data visualization hot registration (2022.10)
- map的一道题目<单词识别>
- Camunda整体架构和相关概念
- C language * Xiaobai's adventure
- MySQL:完整性约束和 表的设计原则
猜你喜欢
随机推荐
再次搞定 Ali 云函数计算 FC
北京大学,新迎3位副校长!其中一人为中科院院士!
iMeta | 德国国家肿瘤中心顾祖光发表复杂热图(ComplexHeatmap)可视化方法
【Idea series】idea configuration
iMeta | 百度认证完成,搜索“iMeta”直达出版社主页和投稿链接
mysql进阶(二十六)MySQL 索引类型
利用pytest hook函数实现自动化测试结果推送企业微信
bitset的基本用法
audio_policy_configuration.xml配置文件详解
如何直击固定资产管理的难题?
Mysql高级篇学习总结13:多表连接查询语句优化方法(带join语句)
Maple 2022软件安装包下载及安装教程
There are 12 balls, including 11 weight, only one, don't know is light or heavy. Three times in balance scales, find out the ball.
Introduction to the core methods of the CompletableFuture interface
Mysql高级篇学习总结14:子查询优化、排序优化、GROUP BY优化、分页查询优化
Learn to use the basic interface of set and map
Camunda overall architecture and related concepts
iMeta | Baidu certification is completed, search "iMeta" directly to the publisher's homepage and submission link
zabbix deployment
少即是多:视觉SLAM的点稀疏化(IROS 2022)









