Skip to content

Commit 0c1e5ec

Browse files
committed
By doing this problem i now understand that there are two things i need to focus on DP and recursion or else ill get fucked in life, true moments
1 parent c8d059e commit 0c1e5ec

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int aheadBuy = 0;
4+
int aheadNotBuy = 0;
5+
6+
int curBuy,curNotbuy;
7+
int maxProfit(vector<int>& prices, int fee)
8+
{
9+
for(int i = prices.size()-1;i>=0;i--)
10+
{
11+
curBuy = max(-prices[i]+aheadNotBuy,0+aheadBuy);
12+
curNotbuy = max(prices[i]+aheadBuy-fee,0+aheadNotBuy);
13+
14+
aheadBuy = curBuy;
15+
aheadNotBuy = curNotbuy;
16+
}
17+
return aheadBuy;
18+
}
19+
};

0 commit comments

Comments
 (0)