Skip to content

Commit dde2979

Browse files
committed
Time: 295 ms (70.99%), Space: 49.1 MB (50.38%) - LeetHub
1 parent e53add9 commit dde2979

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution {
2+
public int MaxProfit(int[] prices)
3+
{
4+
int singleTake = 0, doubleTake = 0, minPrice = prices[0], singleTakeMinusPrice = -1_000_000;
5+
6+
for (var i = 1; i < prices.Length; i++)
7+
{
8+
singleTake = Math.Max(singleTake, prices[i] - minPrice);
9+
doubleTake = Math.Max(doubleTake, prices[i] + singleTakeMinusPrice);
10+
11+
minPrice = Math.Min(minPrice, prices[i]);
12+
singleTakeMinusPrice = Math.Max(singleTakeMinusPrice, singleTake - prices[i]);
13+
}
14+
15+
return Math.Max(singleTake, doubleTake);
16+
}
17+
}

0 commit comments

Comments
 (0)