Skip to content

Instantly share code, notes, and snippets.

@trunkslamchest
Last active August 30, 2020 18:22
Show Gist options
  • Save trunkslamchest/17536adab810d94230b2880153ef7316 to your computer and use it in GitHub Desktop.
Save trunkslamchest/17536adab810d94230b2880153ef7316 to your computer and use it in GitHub Desktop.
blog30_pseudo
// - create 3 variables:
// - sum: an integer. the total sum of nums without any mutations
// - subSum: an integer. the total sum of the subsequence we are building
// - sub: an empty array. the subsequence we want to build, then return at the end of the solution
// - since we need to return a subsequence with the maximum total sum of all its elements
// - sort nums from least to greatest and iterate from the last element to the first
// - this will allow us to add each element of num so that every sum of the subsequence will be the maximum possible sum of a subsequence
// - this will also allow us to build the subsequence in non-increasing order
// since the last number in nums will always be the largest number in nums,
// we can push each element we iterate through into a new array,
// thus ordering the elements in the new array in non increasing order
// - iterate through nums (which is now sorted) backwards
// - if sum is greater than or equal to subSum,
// - add nums[i] to subSum keep track of the sum of the current subsequence
// - subtract nums[i] from sum to keep track of the sum of non-included numbers
// - push nums[i] into sub to build the subsequence
// - otherwise
// - break out of the iteration
// - once sum is less than subSum,
// - it means we have found:
// - a subsequence whose sum of elements is strictly greater than the sum of the non included elements
// - the subsequence with minimum size, even if there are multiple solutions
// - the subsequence with the maximum total sum of all its elements, even if there are multiple solutions
// - all of the above with the elements of sub in non-increasing order
// - return the subsequence once we break out of the for loop
// - return sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment