Skip to content

Instantly share code, notes, and snippets.

@trunkslamchest
Last active August 30, 2020 18:23
Show Gist options
  • Save trunkslamchest/60e1bf1d51859e9dc0b69c4b5bfcca76 to your computer and use it in GitHub Desktop.
Save trunkslamchest/60e1bf1d51859e9dc0b69c4b5bfcca76 to your computer and use it in GitHub Desktop.
blog30_solution5.js
var minSubsequence = function(nums) {
let sum = nums.reduce((a, b) => a + b )
let subSum = 0
let sub = []
nums.sort((a, b) => a - b)
for(i = nums.length - 1; i >= 0; i--){
if(sum >= subSum) {
// add nums[i] to subSum keep track of the sum of the current subsequence
subSum += nums[i]
// subtract nums[i] from sum to keep track of the sum of non-included numbers
sum = sum - nums[i]
// push nums[i] into sub to build the subsequence
sub.push(nums[i])
} else {
break
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment