Skip to content

Commit 69e1c10

Browse files
committed
maximum stock profit complete
1 parent 05efc97 commit 69e1c10

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

.idea/workspace.xml

Lines changed: 16 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

maxStockPrice.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
let maxStockProfit = (priceArray, cb) => {
2+
let maxProfit = -1;
3+
let buyPrice, sellPrice, changeOfBuyPrice = true;
4+
5+
for (let index=0; index< priceArray.length - 1; index++) {
6+
7+
if (changeOfBuyPrice) {
8+
buyPrice = priceArray[index];
9+
}
10+
sellPrice = priceArray[index + 1];
11+
if (sellPrice < buyPrice) {
12+
changeOfBuyPrice = true;
13+
} else {
14+
maxProfit = ((sellPrice - buyPrice) > maxProfit) ? (sellPrice - buyPrice) : maxProfit;
15+
changeOfBuyPrice = false;
16+
}
17+
}
18+
19+
return cb(null, maxProfit);
20+
};
21+
22+
maxStockProfit([32, 46, 26, 38, 40, 48, 42], (err, maxProfit) => {
23+
if (err) {
24+
25+
} else {
26+
console.log(maxProfit);
27+
}
28+
});
29+

0 commit comments

Comments
 (0)