121. 買賣股票的最佳時機 leetcode
阿新 • • 發佈:2018-12-26
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
// 優化頭
while (prices.length>0) {
let maxPrice = Math.max.apply(this,prices);
if (maxPrice<=prices[0]) {
prices.shift();
} else {
break;
}
}
// 優化尾
while (prices.length>0) {
if (prices[prices.length-1]==0) {
prices.pop();
} else {
break;
}
}
// 開始動態規劃填表
let dpTable = [];
for (let i = 0;i<prices.length;i++) {
dpTable[i] = [];
for (let j = 0;j<prices. length;j++) {
if (j<=i) {
dpTable[i].push('x')
} else {
dpTable[i].push(prices[j]-prices[i]);
}
}
}
// 開始動態規劃求值
let maxValue = 0;
for (let i = 0;i<dpTable.length;i++) {
for (let j = i+1;j<dpTable[ i].length;j++) {
if (dpTable[i][j]=='x') {continue;}
if (dpTable[i][j]>maxValue) {
maxValue = dpTable[i][j];
}
}
}
return maxValue;
};