当前位置:网站首页>【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;
}
}
边栏推荐
- 语法基础(变量、输入输出、表达式与顺序语句)
- select 标签自定义样式
- nodeJs--encapsulate routing
- The design idea of DMicro, the Go microservice development framework
- Chinese characters to Pinyin
- The linear table lookup
- 【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
- Images using redis cache Linux master-slave synchronization server hard drive full of moved to the new directory which points to be modified
- 22-07-31周总结
- 2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
猜你喜欢

01 【前言 基础使用 核心概念】

Data storage practice based on left-order traversal

nodeJs--encapsulate routing

Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
![02 [Development Server Resource Module]](/img/60/f77ed0bb0e5654c9dcd70b73a5bee8.png)
02 [Development Server Resource Module]

The Tanabata copywriting you want has been sorted out for you!

C语言实现简单猜数字游戏

Simple description of linked list and simple implementation of code

从零到一快速学会三子棋

Ant Sword Advanced Module Development
随机推荐
select 标签自定义样式
private package
post-study program
lua learning
[机缘参悟-60]:《兵者,诡道也》-2-孙子兵法解读
Note that Weifang generally needs to pay attention to issuing invoices
Tencent Cloud [Hiflow] New Era Automation Tool
How Jin Cang database correctness verification platform installation file
百日刷题计划 ———— DAY2
金仓数据库如何验证安装文件平台正确性
01 [Foreword Basic Use Core Concepts]
QT language file production
语法基础(变量、输入输出、表达式与顺序语句完成情况)
shell statement to modify txt file or sh file
基于左序遍历的数据存储实践
2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
[Fortune-telling-60]: "The Soldier, the Tricky Way"-2-Interpretation of Sun Tzu's Art of War
腾讯云【Hiflow】新时代自动化工具
DAY23: Command Execution & Code Execution Vulnerability
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree