#1403

Minimum Subsequence in Non-Increasing Order

Easy
ArrayGreedySortingGreedySorting
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n log n)
Space
O(1)
O(n)
💡

Intuition

Time O(n log n)Space O(n)

The optimal approach sorts the array in descending order and iteratively adds elements to a subsequence until its sum exceeds the sum of the remaining elements. This is efficient because it directly targets the largest elements first.

⚙️

Algorithm

3 steps
  1. 1Step 1: Sort the array in non-increasing order.
  2. 2Step 2: Initialize two sums: one for the subsequence and one for the remaining elements.
  3. 3Step 3: Iterate through the sorted array, adding elements to the subsequence until its sum is greater than the remaining sum.
solution.py16 lines
1# Full working Python code
2def min_subsequence(nums):
3    nums.sort(reverse=True)
4    total_sum = sum(nums)
5    subseq_sum = 0
6    result = []
7
8    for num in nums:
9        subseq_sum += num
10        result.append(num)
11        if subseq_sum > total_sum - subseq_sum:
12            break
13    return result
14
15# Example usage
16print(min_subsequence([4,3,10,9,8]))  # Output: [10, 9]

Complexity note: The time complexity is O(n log n) due to the sorting step, while the space complexity is O(n) for storing the result subsequence.

  • 1Sorting helps in quickly identifying the largest elements to form a valid subsequence.
  • 2Iteratively adding elements to the subsequence until the condition is met ensures minimal size.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.