Skip to content

Commit 6aadb4b

Browse files
committed
feat: best-time-to-buy-and-sell-stock
1 parent 9ced22c commit 6aadb4b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function maxProfit(prices: number[]): number {
2+
let left = 0;
3+
let right = 1;
4+
let max = 0;
5+
6+
while(left < right && right < prices.length) {
7+
if(prices[right] - prices[left] > 0) {
8+
max = Math.max(max, prices[right] - prices[left])
9+
} else {
10+
left = right;
11+
}
12+
right++;
13+
}
14+
return max;
15+
};

0 commit comments

Comments
 (0)