[LeetCode]#1403. Minimum Subsequence in Non-Increasing Order

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.
  1. Sort it .
  2. Sum it as sum_n (34).
  3. Sum nums[i] as total from maximum to minimal number and add it to list.
  4. If (sum_n-total)<total : break
  5. Get answer.
class Solution:
def minSubsequence(self, nums):
nums.sort(reverse=True)
sum_n=sum(nums)
l=len(nums)
total=0
ans=[]
for i in nums:
total+=i
ans.append(i)
if (sum_n-total)<total:
break
return ans

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.