返回介绍

solution / 1400-1499 / 1403.Minimum Subsequence in Non-Increasing Order / README_EN

发布于 2024-06-17 01:03:20 字数 4873 浏览 0 评论 0 收藏 0

1403. Minimum Subsequence in Non-Increasing Order

中文文档

Description

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 be returned in non-increasing order.  

 

Constraints:

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

Solutions

Solution 1: Sorting

We can first sort the array $nums$ in descending order, then add the elements to the array from largest to smallest. After each addition, we check whether the sum of the current elements is greater than the sum of the remaining elements. If it is, we return the current array.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the length of the array $nums$.

class Solution:
  def minSubsequence(self, nums: List[int]) -> List[int]:
    ans = []
    s, t = sum(nums), 0
    for x in sorted(nums, reverse=True):
      t += x
      ans.append(x)
      if t > s - t:
        break
    return ans
class Solution {
  public List<Integer> minSubsequence(int[] nums) {
    Arrays.sort(nums);
    List<Integer> ans = new ArrayList<>();
    int s = Arrays.stream(nums).sum();
    int t = 0;
    for (int i = nums.length - 1; i >= 0; i--) {
      t += nums[i];
      ans.add(nums[i]);
      if (t > s - t) {
        break;
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> minSubsequence(vector<int>& nums) {
    sort(nums.rbegin(), nums.rend());
    int s = accumulate(nums.begin(), nums.end(), 0);
    int t = 0;
    vector<int> ans;
    for (int x : nums) {
      t += x;
      ans.push_back(x);
      if (t > s - t) {
        break;
      }
    }
    return ans;
  }
};
func minSubsequence(nums []int) (ans []int) {
  sort.Ints(nums)
  s, t := 0, 0
  for _, x := range nums {
    s += x
  }
  for i := len(nums) - 1; ; i-- {
    t += nums[i]
    ans = append(ans, nums[i])
    if t > s-t {
      return
    }
  }
}
function minSubsequence(nums: number[]): number[] {
  nums.sort((a, b) => b - a);
  const s = nums.reduce((r, c) => r + c);
  let t = 0;
  for (let i = 0; ; ++i) {
    t += nums[i];
    if (t > s - t) {
      return nums.slice(0, i + 1);
    }
  }
}
impl Solution {
  pub fn min_subsequence(mut nums: Vec<i32>) -> Vec<i32> {
    nums.sort_by(|a, b| b.cmp(a));
    let sum = nums.iter().sum::<i32>();
    let mut res = vec![];
    let mut t = 0;
    for num in nums.into_iter() {
      t += num;
      res.push(num);
      if t > sum - t {
        break;
      }
    }
    res
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文