This commit is contained in:
12
src/s0122_best_time_to_buy_and_sell_stock_ii.cpp
Normal file
12
src/s0122_best_time_to_buy_and_sell_stock_ii.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "s0122_best_time_to_buy_and_sell_stock_ii.hpp"
|
||||
|
||||
int S0122::maxProfit(vector<int>& prices) {
|
||||
int len = prices.size();
|
||||
vector<vector<int>> dp(len, vector<int>{0, 0});
|
||||
dp[0][0] = -prices[0];
|
||||
for (int i{1}; i < len; ++i) {
|
||||
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
|
||||
dp[i][1] = max(dp[i - 1][0] + prices[i], dp[i - 1][1]);
|
||||
}
|
||||
return max(dp[len - 1][1], 0);
|
||||
}
|
Reference in New Issue
Block a user