当前位置:网站首页>【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;
}
}
边栏推荐
- [ROS](10)ROS通信 —— 服务(Service)通信
- [深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)
- 人人都在说的数据中台,你需要关注的核心特点是什么?
- Introduction to SDC
- RAID disk array
- 01 [Foreword Basic Use Core Concepts]
- sql server installation prompts that the username does not exist
- LeetCode使用最小花费爬楼梯----dp问题
- Lexicon - the maximum depth of a binary tree
- LeetCode uses the minimum cost to climb the stairs----dp problem
猜你喜欢
LeetCode使用最小花费爬楼梯----dp问题
select tag custom style
VSCode Change Default Terminal how to modify the Default Terminal VSCode
The linear table lookup
dmp(dump)转储文件
你要的七夕文案,已为您整理好!
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
[ROS](10)ROS通信 —— 服务(Service)通信
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
随机推荐
leetcode 15
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
Study Notes-----Left-biased Tree
语法基础(变量、输入输出、表达式与顺序语句完成情况)
C语言实现简单猜数字游戏
你要的七夕文案,已为您整理好!
(11) Metaclass
Everyone in China said data, you need to focus on core characteristic is what?
Syntax basics (variables, input and output, expressions and sequential statements)
Multithreading (2)
torch.roll()
02 【开发服务器 资源模块】
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
Cloud Native (32) | Introduction to Platform Storage System in Kubernetes
人人都在说的数据中台,你需要关注的核心特点是什么?
Physical backup issues caused by soft links
Syntax basics (variables, input and output, expressions and sequential statements)
剑指offer专项突击版第20天
QStyle平台风格
[深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)