当前位置:网站首页>LeetCode Question of the Day - 1403. Minimum Subsequence in Non-Increasing Order
LeetCode Question of the Day - 1403. Minimum Subsequence in Non-Increasing Order
2022-08-04 17:03:00 【SK_Jaco】
1.题目描述
给你一个数组 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]
2.解题思路与代码
2.1 解题思路
这道题非常简单,The question asks to find the subsequence with the smallest length,And the sum of the subsequences is required to be greater than the sum of the elements outside the subsequence,Then we can think of if the sum of the subsequences is greater than half of the sum of the whole array,Then the sum of this subsequence must be greater than the sum of the rest of the elements.Start with this idea,我们先对数组进行排序,Then start traversing from the position with the largest number.The condition that the subsequence length required by the title is minimum and decreasing,Then we start summing from high,Exit until the sum is greater than half the sum of the array for the first time,This will give you the results.
以 [4,3,10,9,8] 为例,First we sort the array,And find the sum of the array as 34,Then we just need to find greater than 17 的子序列即可
The title requires subsequences in descending order and minimum length,Since the sorting is sorted from small to large,Then we traverse forward from the last bit of the array.when pointing to the last digit,The sum of the subsequences at this time is 10 小于 17.Because the title requires the result list to be sorted in descending order,因此将 10 放入结果列表中,然后继续向前移动
after moving forward,The sum of the subsequences is 19 大于 17 满足条件,将 9 Exit the loop after putting in the result list,At this point the subsequence in the resulting list is [10, 9]
2.2 代码
class Solution {
public List<Integer> minSubsequence(int[] nums) {
int sum = Arrays.stream(nums).sum() >> 1;
Arrays.sort(nums);
List<Integer> ans = new ArrayList<>();
int i = nums.length - 1;
int tmp = 0;
while (i >= 0) {
tmp += nums[i];
ans.add(nums[i]);
if (tmp > sum) {
break;
}
i--;
}
return ans;
}
}ji
2.3 测试结果
通过测试

3.总结
- 对数组进行排序,Then start traversing from the high position
- Find a sequence that is greater than the array value and half
边栏推荐
猜你喜欢
随机推荐
Minecraft HMCL 第三方启动器使用教程
生产环境重大bug,update加上索引字段会走索引进行更新?还是走全表扫描
理财产品买入后份额是固定不变的吗?
GraphQL 入门与实践
聚合收款码有限制吗?怎么办理?
15天升级打怪,成为虚拟时尚创作者
容器化 | 在 NFS 备份恢复 RadonDB MySQL 集群数据
Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World。
WEB 渗透之逻辑漏洞
移动魔百盒CM211-1_YS代工_S905L3B_RTL8822C_线刷固件包
小满nestjs(第一章 介绍nestjs)
Hubei Mobile HG680-LV_S905L3B_wire brush firmware package
Qt自动补全之QCompleter使用
移动魔百盒CM201-1_CW_S905L2_MT7668_线刷固件包
御神楽的学习记录之基于FPGA的AHT10温湿度数据采集
15 days to upgrade to fight monsters and become a virtual fashion creator
移动海信IP102H_905L3-B_线刷固件包
移动百事通BesTV_R3300-L_S905L_8189_线刷固件包
移动平台助力推进智慧型科研院所信息化建设
移动CM101s_MV100_EMMC_M8233_强刷后全分区线刷固件包









