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]

这题我们可以先进行一次从大到小的排序,计算出它的总和。然后不断的进行尝试,拿掉一个最大的,看看是否符合条件,符合即返回即可。

class MinimumSubsequenceInNonIncreasingOrder : public Solution {
public:
    void Exec() {

    }
    vector<int> minSubsequence(vector<int>& nums) {
        std::sort(nums.rbegin(), nums.rend());
        int totalSum = 0;
        for (auto v : nums) {
            totalSum += v;
        }
        vector<int> res;
        int sum = 0;
        for (auto v : nums) {
            res.push_back(v);
            sum += v;
            int left = totalSum - sum;
            if (sum > left) {
                break;
            } 
        }
        return res;
    }
};
共 0 条回复
暂时没有人回复哦,赶紧抢沙发
发表新回复

作者

sryan
today is a good day