当前位置:网站首页>【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;
}
}
边栏推荐
- Solve connect: The requested address is not valid in its context
- torch.roll()
- 虚拟内存原理与技术
- mysql tree structure query problem
- 01 【前言 基础使用 核心概念】
- In 2022, you still can't "low code"?Data science can also play with Low-Code!
- post-study program
- Tencent Cloud [Hiflow] New Era Automation Tool
- Common hardware delays
- mysql can't Execute, please solve it
猜你喜欢
随机推荐
QT language file production
1873. The special bonus calculation
Regular expression to match a certain string in the middle
Go 微服务开发框架 DMicro 的设计思路
The 20th day of the special assault version of the sword offer
常见的硬件延迟
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
Pisanix v0.2.0 released | Added support for dynamic read-write separation
Matlab画图3
【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
金仓数据库如何验证安装文件平台正确性
【LeetCode刷题】-数之和专题(待补充更多题目)
Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!
Solve connect: The requested address is not valid in its context
语法基础(变量、输入输出、表达式与顺序语句完成情况)
1484. 按日期分组销售产品
数据增强Mixup原理与代码解读
[ROS](10)ROS通信 —— 服务(Service)通信
01 [Foreword Basic Use Core Concepts]
sql server installation prompts that the username does not exist