Minimum Subsequence in Non-Increasing Order — LeetCode 1403 Python Solution
- Problem
- #1403
- Pattern
- Greedy
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- 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.
Python solution
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 ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n \times \log n) |
| Space | O(\log n) auxiliary |
Pattern: Greedy
Take the locally best option every time — when you can prove that never costs you later. LeetCode 1403. Minimum Subsequence in Non-Increasing Order is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Greedy.
The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1403. Minimum Subsequence in Non-Increasing Order?
- LeetCode 1403. Minimum Subsequence in Non-Increasing Order is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1403. Minimum Subsequence in Non-Increasing Order?
- The Python solution on this page runs in O(n \times \log n).
- What is the space complexity of LeetCode 1403. Minimum Subsequence in Non-Increasing Order?
- The Python solution on this page uses O(\log n) auxiliary space.
- What topics does LeetCode 1403. Minimum Subsequence in Non-Increasing Order cover?
- LeetCode 1403. Minimum Subsequence in Non-Increasing Order is tagged Greedy, Array and Sorting on LeetCode.