/**
* 最大利润
*/
interface ProFitI {
buy: number;
sale: Number;
currentProFit: number;
}
const maxProFit = (price: number[]) => {
let proFit:ProFitI[] = []
let maxProFit = 0
for(let i=0; i<price.length; i++){
for(let j=i+1; j<price.length; j++){
let currentProFit = price[j] - price[i]
if(currentProFit > maxProFit) maxProFit = currentProFit
proFit.push({
buy:price[i],
sale: price[j],
currentProFit
})
}
}
const maxArray: ProFitI[] = proFit.filter(ele=> ele.currentProFit == maxProFit)
console.log(maxArray.map(maxItem => {
return `当买入价格为${maxItem.buy},卖出价格为${maxItem.sale},利润最大${maxItem.currentProFit}`
}).join("\n"))
return maxProFit
}
console.log(maxProFit([1,2,5,2,1,5]))
求一组股票价格的最大利润
最新推荐文章于 2024-07-12 16:27:40 发布