forked from bijucdevassy/codeathon-sep23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumStockProfit.js
37 lines (30 loc) · 1.02 KB
/
MaximumStockProfit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function findMaxProfit(prices) {
let minPrice = prices[0];
let maxProfit = 0;
let buyIndex = 0;
let sellIndex = 0;
for (let i = 1; i < prices.length; i++) {
const currentPrice = prices[i];
const potentialProfit = currentPrice - minPrice;
if (potentialProfit > maxProfit) {
maxProfit = potentialProfit;
sellIndex = i;
}
if (currentPrice < minPrice) {
minPrice = currentPrice;
buyIndex = i;
}
}
if (maxProfit > 0) {
const buyPrice = prices[buyIndex];
const sellPrice = prices[sellIndex];
return `Buy on day ${buyIndex + 1} at price ${buyPrice}\nSell on day ${sellIndex + 1} at price ${sellPrice}\nMax profit: ${maxProfit}`;
} else {
return -1;
}
}
console.log(findMaxProfit([7,1,5,3,6,4]));
console.log(findMaxProfit([7,6,4,3,1]));
console.log(findMaxProfit([1,2,3,4,5]));
console.log(findMaxProfit([2,1,2,1,0,1,2]));
console.log(findMaxProfit([2,1,2,1,0,1,2]));