Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

maximum sum subarray javascript

const maxSubArray = (nums) => {
    // initiate two variable, maxSum for total max, sum for current max
    let maxSum = -Infinity
    let currentSum = 0
    // iterate through the nums, store sub-problems result
    for(let i = 0; i < nums.length; i++){ 
        //cumulating answers to the top
        
        //compare currentSum add current number 
        //with current number and store the maximum value
        currentSum = Math.max(nums[i], currentSum + nums[i])
        
        //compare maxSum with currentSum and store the greater value
        maxSum = Math.max(currentSum, maxSum)
        
    }
    return maxSum
}
Comment

largest sum contiguous subarray javascript

Using kadane's algorithm

const maxSubArray = (nums) => {
    // initiate two variable, maxSum for total max, sum for current max
    let maxSum = -Infinity
    let currentSum = 0
    // iterate through the nums, store sub-problems result
    for(let i = 0; i < nums.length; i++){ 
        //cumulating answers to the top
        
        //compare currentSum add current number 
        //with current number and store the maximum value
        currentSum = Math.max(nums[i], currentSum + nums[i])
        
        //compare maxSum with currentSum and store the greater value
        maxSum = Math.max(currentSum, maxSum)
        
    }
    return maxSum
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript auto scroll a page to top 
Javascript :: header disallowed by preflight response in express 
Javascript :: delete all objects in array of objects with specific attribute 
Javascript :: laravel ajax post data to controller 
Javascript :: convert a new date standard to a yyy-mm-dd format in javascript 
Javascript :: how to sort an array of object 
Javascript :: string to date js 
Javascript :: react dont render component until loaded 
Javascript :: normalize method javascript 
Javascript :: get value from textbox in vanilla javascript 
Javascript :: connect mysql to node js 
Javascript :: form.select not working react bootstrap 
Javascript :: how to get connection string value from appsettings.json in .net core 
Javascript :: youtube set speed command 
Javascript :: Moment.js: Date between dates 
Javascript :: sort arrays according to first array js 
Javascript :: vuejs get value of checkbox group 
Javascript :: javascript dump strack trace 
Javascript :: javascript convert to array 
Javascript :: .on click jquery 
Javascript :: Send Post Fetch REquest With Django 
Javascript :: netmask /24 
Javascript :: find js 
Javascript :: bin2hex in js 
Javascript :: filter repetition 2d array javascript 
Javascript :: react i18n outside component 
Javascript :: how to make a popup in javascript -html 
Javascript :: vue router guard 
Javascript :: warning React Hook useEffect has a missing dependency: 
Javascript :: codeblocks in html cdnjs 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =