zoukankan      html  css  js  c++  java
  • 1403. 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
    class Solution {
        public List<Integer> minSubsequence(int[] nums) {
            List<Integer> res = new ArrayList();
            int sum = 0;
            for(int i: nums) sum += i;
            Arrays.sort(nums);
            int count = 0;
            for(int i = nums.length - 1; i >= 0; i--){
                count += nums[i];
                res.add(nums[i]);
                if(count > sum / 2) return res;
            }
            return res;
        }
    }

    障眼法:subsequence

    实际操作只要排序,然后加大的直到和大于sum/2就可以了

    想想也是,不管排不排序都可以组成subsequence

  • 相关阅读:
    uplift model学习笔记
    TensorFlow、Numpy中的axis的理解
    JStorm与Storm源码分析(六)--收集器 IOutputCollector 、OutputCollector
    JStorm与Storm源码分析(一)--nimbus-data
    控制反转(IoC)-解析与实现
    ICA独立成分分析去除EEG伪影
    Boston和MIT研究人员利用脑电信号实时控制机器人
    利用LSTM(长短期记忆网络)来处理脑电数据
    EEG数据、伪影的查看与清洗
    EEG vs MRI vs fMRI vs fNIRS简介
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13090861.html
Copyright © 2011-2022 走看看