1403. Minimum Subsequence in Non-Increasing Order LeetCode Solution

In this guide, you will get 1403. Minimum Subsequence in Non-Increasing Order LeetCode Solution with the best time and space complexity. The solution to Minimum Subsequence in Non-Increasing Order problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Subsequence in Non-Increasing Order solution in C++
  4. Minimum Subsequence in Non-Increasing Order solution in Java
  5. Minimum Subsequence in Non-Increasing Order solution in Python
  6. Additional Resources
1403. Minimum Subsequence in Non-Increasing Order LeetCode Solution image

Problem Statement of 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.

See also  373. Find K Pairs with Smallest Sums LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(n\log n)
  • Space Complexity: O(n)

1403. Minimum Subsequence in Non-Increasing Order LeetCode Solution in C++

class Solution {
 public:
  vector<int> minSubsequence(vector<int>& nums) {
    vector<int> ans;
    priority_queue<int> maxHeap(nums.begin(), nums.end());
    int half = accumulate(nums.begin(), nums.end(), 0) / 2;

    while (half >= 0) {
      ans.push_back(maxHeap.top());
      half -= maxHeap.top(), maxHeap.pop();
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1403. Minimum Subsequence in Non-Increasing Order LeetCode Solution in Java

class Solution {
  public List<Integer> minSubsequence(int[] nums) {
    List<Integer> ans = new ArrayList<>();
    Queue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
    for (final int num : nums)
      maxHeap.offer(num);
    int half = Arrays.stream(nums).sum() / 2;

    while (half >= 0) {
      ans.add(maxHeap.peek());
      half -= maxHeap.poll();
    }

    return ans;
  }
}
// code provided by PROGIEZ

1403. Minimum Subsequence in Non-Increasing Order LeetCode Solution in Python

class Solution:
  def minSubsequence(self, nums: list[int]) -> list[int]:
    ans = []
    maxHeap = [-num for num in nums]
    heapq.heapify(maxHeap)
    half = sum(nums) // 2

    while half >= 0:
      ans.append(-maxHeap[0])
      half += heapq.heappop(maxHeap)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.