题解 | #买卖股票的最好时机(一)#
买卖股票的最好时机(一)
http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
/**
*
* @param prices int整型一维数组
* @return int整型
*/
function maxProfit( prices ) {
// 2022 0222
let max = 0;
let minPrice = prices[0];
for(let i = 1; i < prices.length; i++){
minPrice = Math.min(minPrice, prices[i]);
max = Math.max(max, prices[i] - minPrice);
}
return max;
}
module.exports = {
maxProfit : maxProfit
};
*
* @param prices int整型一维数组
* @return int整型
*/
function maxProfit( prices ) {
// 2022 0222
let max = 0;
let minPrice = prices[0];
for(let i = 1; i < prices.length; i++){
minPrice = Math.min(minPrice, prices[i]);
max = Math.max(max, prices[i] - minPrice);
}
return max;
}
module.exports = {
maxProfit : maxProfit
};