当前位置:网站首页>【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
2022-08-05 02:47: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 {
// Sort from back to frontlist,直到listThe sum of the median numbers is greater than halfsum
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;
}
}
边栏推荐
- The linear table lookup
- LeetCode uses the minimum cost to climb the stairs----dp problem
- (11) Metaclass
- 人人都在说的数据中台,你需要关注的核心特点是什么?
- mysql tree structure query problem
- word column notes
- Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
- QT语言文件制作
- 金仓数据库如何验证安装文件平台正确性
- C language diary 9 3 kinds of statements of if
猜你喜欢
随机推荐
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
Solve the problem of port occupancy Port xxxx was already in use
云原生(三十二) | Kubernetes篇之平台存储系统介绍
OpenGL 工作原理
How Jin Cang database correctness verification platform installation file
注意潍坊开具发票一般需要注意
软链接引发的物理备份问题
Tencent Cloud [Hiflow] New Era Automation Tool
QT MV\MVC结构
Gantt chart is here, project management artifact, template is used directly
(十一)元类
Everyone in China said data, you need to focus on core characteristic is what?
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
Programmer's Tanabata Romantic Moment
【OpenCV 图像处理2】:OpenCV 基础知识
C语言日记 9 if的3种语句
[深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)
matlab绘制用颜色表示模值大小的箭头图
正则表达式,匹配中间的某一段字符串
2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。