1403. Minimum Subsequence in Non-Increasing Order

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

C++解法

class Solution {
public:
    vector<int> minSubsequence(vector<int>& nums) {
        vector<int> result;
        vector<int> hash(101);
        int sum=0;
        for(auto it : nums){
            sum+=it;
            hash[it]++;
        }
        int temp=0;
        for(auto i=100;i>=1;i--){
            while(hash[i]){
                hash[i]--;
                temp+=i;
                result.push_back(i);
                if(temp>sum-temp){
                    return result;
                }
            }
        }
        return result;
    }
};

Java解法

class Solution {
    public List<Integer> minSubsequence(int[] nums) {
        int[] hash=new int[101];
        int sum=0;
        for(int i : nums){
            sum+=i;
            hash[i]++;
        }
        List<Integer> result=new ArrayList<Integer>();
        int temp=0;
        for(int i=100;i>=1;i--){
            while(hash[i]>0){
                temp+=i;
                hash[i]--;
                result.add(i);
                if(temp>sum-temp)
                    return result;
            }
        }
        return result;
    }
}

Go解法

func minSubsequence(nums []int) []int {
    hash := make([]int,101)
    sum := 0
    for _,v := range nums {
        sum += v
        hash[v]++
    }
    
    temp := 0
    var result []int
    for i:=100;i>=1;i-- {
        for hash[i]>0 {
            temp += i
            result=append(result,i)
            hash[i]--
            if temp > sum-temp{
                return result;
            }
        }
    }
    return result
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值