加载中...
1403-非递增顺序的最小子序列(Minimum Subsequence in Non-Increasing Order)
发表于:2021-12-03 | 分类: 简单
字数统计: 930 | 阅读时长: 4分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/minimum-subsequence-in-non-increasing-order

英文原文

Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence. 

If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions, return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. 

Note that the solution with the given constraints is guaranteed to be unique. Also return the answer sorted in non-increasing order.

 

Example 1:

Input: nums = [4,3,10,9,8]
Output: [10,9] 
Explanation: The subsequences [10,9] and [10,8] are minimal such that the sum of their elements is strictly greater than the sum of elements not included, however, the subsequence [10,9] has the maximum total sum of its elements. 

Example 2:

Input: nums = [4,4,7,6,7]
Output: [7,7,6] 
Explanation: The subsequence [7,7] has the sum of its elements equal to 14 which is not strictly greater than the sum of elements not included (14 = 4 + 4 + 6). Therefore, the subsequence [7,6,7] is the minimal satisfying the conditions. Note the subsequence has to returned in non-decreasing order.  

Example 3:

Input: nums = [6]
Output: [6]

 

Constraints:

  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 100

中文题目

给你一个数组 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

通过代码

高赞题解

5376. 非递增顺序的最小子序列

将数组 nums 划分为两个子序列 A, B,A 要满足下述两个条件:

  • sum(A) > sum(B)
  • A 的长度要尽可能的短。

当把 nums 中的所有元素都给 A 时,显然满足第一个条件,因为 nums 中只有正整数。
然后不断的将 A 中最小的元素转移到 B 中,直到无法转移。无法转移的意思是,再转移一个元素就会打破第一条限制。此时 降序排序的 A 即为答案。
实现代码如下:

class Solution {
public:
    vector<int> minSubsequence(vector<int>& nums) {
        sort(nums.begin(), nums.end(), greater<int>());
        int sum = 0; 
        for(auto v : nums) {
            sum += v;
        }
        int ts = 0;
        for(int i = 0; i < nums.size(); i++) {
            ts += nums[i];
            if(ts > sum - ts) {
                return vector<int>(nums.begin(), nums.begin() + i + 1);
            }
        }
        return nums;
    }
};

如果感觉有点意思,可以关注👏HelloNebula👏

  • 分享周赛题解
  • 分享计算机专业课知识
  • 分享C++相关岗位面试题
  • 分享专业书籍PDF

统计信息

通过次数 提交次数 AC比率
21979 31865 69.0%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1415-长度为 n 的开心字符串中字典序第 k 小的字符串(The k-th Lexicographical String of All Happy Strings of Length n)
下一篇:
1404-将二进制表示减到 1 的步骤数(Number of Steps to Reduce a Number in Binary Representation to One)
本文目录
本文目录