Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/solution/mai-mai-gu-piao-de-zui-jia-shi-ji-ii-by-leetcode-s/
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
class Solution
{
public:
int maxProfit(vector<int>& prices)
{
int ans = 0;
for (int i = 1; i < prices.size(); ++i)
{
ans += max(0,prices[i]-prices[i-1]);
}
return ans;
}
};
int main(int argc, char const *argv[])
{
Solution s;
vector<int> prices = {7,1,5,3,6,4};
// vector<int> prices = {1,2,3,4,5};
// for (auto i:prices)
// printf("%d\n", i);
printf("%d\n", s.maxProfit(prices));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.