最大和子数组-题号未知

const arrSum = (arr) => {
    return arr.reduce((a, b) => {
        return a+b
    }, 0)
}

const f = (arr) => {
    // .....
    let max = -Infinity
    for (let i = 0;i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            let newVal = arrSum(arr.slice(i, j+1)) 
            max = max < newVal? newVal: max 
        }
    }
    return max
}

 

posted on 2021-09-09 09:50  hhvfg  阅读(25)  评论(0)    收藏  举报