当前位置:网站首页>【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;
}
}
边栏推荐
- 【LeetCode刷题】-数之和专题(待补充更多题目)
- 采用redis缓存的linux主从同步服务器图片硬盘满了移到新目录要修改哪些指向
- tree table lookup
- 02 [Development Server Resource Module]
- Cloud Native (32) | Introduction to Platform Storage System in Kubernetes
- Data to enhance Mixup principle and code reading
- QT:神奇QVarient
- 正则表达式,匹配中间的某一段字符串
- 22-07-31周总结
- 你要的七夕文案,已为您整理好!
猜你喜欢

Pisanix v0.2.0 released | Added support for dynamic read-write separation

Cloud Native (32) | Introduction to Platform Storage System in Kubernetes

Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium

【LeetCode刷题】-数之和专题(待补充更多题目)

How OpenGL works

QT language file production

云原生(三十二) | Kubernetes篇之平台存储系统介绍

Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!

lua learning

Study Notes-----Left-biased Tree
随机推荐
In 2022, you still can't "low code"?Data science can also play with Low-Code!
Matlab drawing 3
[In-depth study of 4G/5G/6G topic-51]: URLLC-16-"3GPP URLLC related protocols, specifications, and technical principles in-depth interpretation"-11-High reliability technology-2-Link adaptive enhancem
mysql没法Execute 大拿们求解
QT语言文件制作
2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
mysql can't Execute, please solve it
C student management system head to add a student node
QStyle platform style
Everyone in China said data, you need to focus on core characteristic is what?
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
Chinese characters to Pinyin
lua学习
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
C language diary 9 3 kinds of statements of if
1527. Patients suffering from a disease
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
语法基础(变量、输入输出、表达式与顺序语句)
Gantt chart is here, project management artifact, template is used directly
正则表达式,匹配中间的某一段字符串