Skip to content

Commit

Permalink
Merge pull request #311 from Rabbittee/feature/day38_hello_solution
Browse files Browse the repository at this point in the history
add hello solution
  • Loading branch information
jiunting authored Aug 26, 2021
2 parents 8af40a0 + d2bbfa3 commit 38fe757
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 2021-08/2021-08-25 (day38)/Hello/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { max, min } = Math;

/**
* @param {number[]} prices
* @return {number}
*/
function maxProfit(prices) {
let firstSell = 0;
let firstBuy = Infinity;

let secondSell = 0;
let secondBuy = Infinity;

for (const price of prices) {
firstBuy = min(firstBuy, price);
firstSell = max(firstSell, price - firstBuy);

secondBuy = min(secondBuy, price - firstSell);
secondSell = max(secondSell, price - secondBuy);
}

return secondSell;
}

0 comments on commit 38fe757

Please sign in to comment.