Last active
August 30, 2020 18:22
-
-
Save trunkslamchest/17536adab810d94230b2880153ef7316 to your computer and use it in GitHub Desktop.
blog30_pseudo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// - 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