Minimum Subsequence in Non-Increasing Order - Problem
๐ฏ Find the Minimum Subsequence with Maximum Impact
You're given an array nums and need to find a special subsequence that packs the biggest punch with the smallest size!
Your mission: Find a subsequence where the sum of its elements is strictly greater than the sum of all the elements you left behind.
The twist: If multiple subsequences work, you want:
- ๐ Minimum size (fewer elements preferred)
- ๐ฐ Maximum sum (if sizes are equal, pick the one with larger total)
- ๐ Non-increasing order (return result sorted from largest to smallest)
Example: Given [4,3,10,9,8], you could pick [10,9] with sum 19, which is greater than the remaining sum [4,3,8] = 15. Perfect! โจ
The problem guarantees a unique solution exists, so no need to worry about ties!
Input & Output
example_1.py โ Basic Case
$
Input:
[4,3,10,9,8]
โบ
Output:
[10,9]
๐ก Note:
The sum of [10,9] is 19, which is greater than the sum of remaining elements [4,3,8] = 15. This is the minimum size subsequence that satisfies the condition.
example_2.py โ Tie-breaking
$
Input:
[4,4,7,6,7]
โบ
Output:
[7,7,6]
๐ก Note:
Multiple subsequences work, but [7,7,6] has sum 20 > remaining 8, and it's the optimal choice following our criteria (minimum size, maximum sum).
example_3.py โ Single Element
$
Input:
[6]
โบ
Output:
[6]
๐ก Note:
With only one element, we must take it. Sum 6 > remaining 0, so [6] is the answer.
Constraints
- 1 โค nums.length โค 500
- 1 โค nums[i] โค 100
- The answer is guaranteed to be unique
- The subsequence must be returned in non-increasing order
Visualization
Tap to expand
Understanding the Visualization
1
Arrange by Value
Line up all treasure coins from most valuable to least valuable
2
Greedy Selection
Pick coins one by one, always choosing the most valuable remaining coin
3
Check Progress
After each coin, check if you have more than the remaining treasure
4
Stop Condition
Stop as soon as your coins are worth more than what's left behind
Key Takeaway
๐ฏ Key Insight: The greedy approach works because taking the largest elements first always leads to the minimum subsequence size needed to satisfy the sum condition, automatically maximizing the sum for any given size.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code