当前位置:网站首页>【日常训练】1403. 非递增顺序的最小子序列
【日常训练】1403. 非递增顺序的最小子序列
2022-08-05 01:59:00 【Puppet__】
题目
给你一个数组 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 {
// 排序从后往前加入list,直到list中数的和大于一半sum
public List<Integer> minSubsequence(int[] nums) {
Arrays.sort(nums);
List<Integer> ansList = new ArrayList<>();
int sum = 0;
for(int num : nums){
sum += num;
}
int tmp = 0;
for(int i = nums.length - 1; i >=0; i--){
tmp += nums[i];
ansList.add(nums[i]);
if(tmp * 2 > sum){
break;
}
}
return ansList;
}
}
边栏推荐
- Fragment visibility judgment
- 程序员失眠时的数羊列表 | 每日趣闻
- Leetcode刷题——22. 括号生成
- 【Word】Word公式导出PDF后出现井号括号#()错误
- STM32使用stm32cubemx LL库系列教程
- "Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory
- AI+小核酸药物|Eleven完成2200万美元种子轮融资
- SAP ERP和ORACLE ERP的区别是哪些?
- 迅睿cms网站搬迁换了服务器后网站不能正常显示
- Transfer Learning - Distant Domain Transfer Learning
猜你喜欢

A new technical director, who calls DDD a senior, is convinced

(十七)51单片机——AD/DA转换

一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
![[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?](/img/eb/535ffaff9b535fbc73a4d56aab0b3a.png)
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?

.Net C# 控制台 使用 Win32 API 创建一个窗口

蓝牙Mesh系统开发四 ble mesh网关节点管理

【TA-霜狼_may-《百人计划》】图形4.3 实时阴影介绍

关于#sql shell#的问题,如何解决?

硬实力和软实力,哪个对测试人来说更重要?

HOG feature study notes
随机推荐
The use of pytorch: temperature prediction using neural networks
Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
XMjs跨域问题解决
2022杭电多校第一场
【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
pytorch的使用:卷积神经网络模块
MySQL learning
Three handshake and four wave in tcp
tcp中的三次握手与四次挥手
2022 EdgeX中国挑战赛8月3日即将盛大开幕
【PyQT5 绑定函数的传参】
意识形态的机制
《.NET物联网从零开始》系列
ExcelPatternTool: Excel table-database mutual import tool
[GYCTF2020]EasyThinking
转:查尔斯·汉迪:你是谁,比你做什么更重要
多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)
树形查找(二叉查找树)
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?