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
// SUMMARY: | |
// This Immidiately Invoked Function Constructor is designed to abstract/boilerplate basic JS GET/POST requests, | |
// specifically with React & Redux. | |
// | |
// I wrote a 7 part blog series detailing how this file works: | |
// - https://levelup.gitconnected.com/function-construction-whats-your-function-5a282b81fc62 | |
// | |
// USAGE: | |
// - import this file into your component: | |
// - import fetchFunctions from <your path to the file> |
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
var minSubsequence = function(nums) { | |
let sum = nums.reduce((a, b) => a + b ), subSum = 0, sub = [] | |
nums.sort((a, b) => a - b) | |
for(i = nums.length - 1; i >= 0; i--){ | |
if(sum >= subSum) { | |
subSum += nums[i] | |
sum = sum - nums[i] | |
sub.push(nums[i]) | |
} else break |
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
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) { | |
subSum += nums[i] |
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
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) { | |
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
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 is greater than or equal to subSum |
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
var minSubsequence = function(nums) { | |
let sum = nums.reduce((a, b) => a + b ) | |
let subSum = 0 | |
let sub = [] | |
nums.sort((a, b) => a - b) | |
// iterate through nums (which is now sorted) backwards | |
for(i = nums.length - 1; i >= 0; i--){ |
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
var minSubsequence = function(nums) { | |
let sum = nums.reduce((a, b) => a + b ) | |
let subSum = 0 | |
let sub = [] | |
// sort nums from least to greatest | |
nums.sort((a, b) => a - b) | |
}; |
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
var minSubsequence = function(nums) { | |
// sum: an integer. the total sum of nums without any mutations | |
let sum = nums.reduce((a, b) => a + b ) | |
// the total sum of the subsequence we are building | |
let subSum = 0 | |
// the subsequence we want to build, then return at the end of the solution | |
let sub = [] |
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, |
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
Test Case #1: | |
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. | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
NewerOlder