陣列左邊減去右邊數值的最大差值

HeyZero發表於2018-10-16

思路1:

  • 窮舉法

思路2:

function maxValue (arr) {
    let min = arr[arr.length-1]; 
    let _maxValue = arr[arr.length - 2] - min; 
    let diff = 0;
    for( let i = arr.length - 3; i>=0; i--){
        if ( arr[i+1] < min ) {
            min = arr[i+1];
        }

        diff = arr[i] - min;
        if (diff > _maxValue) {
            _maxValue = diff;
        }
    }
    return _maxValue;
}
複製程式碼

相關文章