Find max sub array

  • Key point
    1. Sub array : continuous index in the array with at least one element
  • Approaches
    1. Brute-force

// My brute force solution
var a = [7, -1, -2, -4, 5, 3, -10, -1, 20]; 
console.log(findMax(a));

function findMax(a){
    var max = {
        val : undefined,
        index : undefined,
    };

    for(var i=0; i=i){
        for(var k=i; k<=j; k++){
            sum += a[k];
        }
    }
    return sum;
}

  • Learning resources:
    https://www.youtube.com/watch?v=ohHWQf1HDfU&index=15&list=PL2_aWCzGMAwLPEZrZIcNEq9ukGWPfLT4A

你可能感兴趣的:(Find max sub array)