LeetCode 121. Best Time to Buy and Sell Stock
Solution1:我的答案
动态规划和贪心不要区分的那么明显嘛~~~
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (n <= 1) return 0;
int max_pro = 0;
int dp[n];
dp[0] = 0;
for (int i = 1; i < n; i++) {
dp[i] = max(prices[i] - prices[i-1] + dp[i-1], 0);
max_pro = max(dp[i], max_pro);
}
return max_pro;
}
};