题解 | NC7 买卖股票的最好时机(一)
买卖股票的最好时机(一)
http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
贪心法
*
* @param prices int整型一维数组
* @return int整型
*/
function maxProfit( prices ) {
// write code here
let n = prices.length;
if(n <=1) return 0;
let minValue = prices[0],maxP = 0;
for(let i = 0;i < n;i++){
minValue = Math.min(minValue,prices[i]);
maxP = Math.max(maxP,prices[i] - minValue);
}
return maxP;
}
module.exports = {
maxProfit : maxProfit
};