Skip to content

Instantly share code, notes, and snippets.

@leonidkuznetsov18
Last active June 11, 2020 11:45
Show Gist options
  • Select an option

  • Save leonidkuznetsov18/2426da0975016bb2ecf536471b33ff29 to your computer and use it in GitHub Desktop.

Select an option

Save leonidkuznetsov18/2426da0975016bb2ecf536471b33ff29 to your computer and use it in GitHub Desktop.
maximum sub arrays
function maxSubArray(nums) {
let currSum = -Infinity;
let maxSum = -Infinity;
for (let i = 0; i < nums.length; i++) {
currSum = Math.max(0, currSum);
currSum +=nums[i];
maxSum = Math.max(maxSum, currSum);
}
return maxSum;
};
// Big O Analysis:
// Time: O(n)
// A simple linear scan through an array.
// Space: O(1)
// No memory does not scale based on input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment