leetcode 买卖股票的最佳时机Ⅱ

因为可以进行多次买卖,这是一个贪心问题,只要当天股票价格比前一天股票价格上涨,便卖出

#include <vector>
using namespace std;
class Solution {
public:
    int maxProfit(vector<int>& prices) 
    {
       int n = prices.size();
       int ans = 0;
       for( int i = 1; i < n; i++ )
       {
           if( prices[i] > prices[i-1] )
           {
               res += (prices[i] - prices[i-1]);
           }
       }
       return res;
    }
};

猜你喜欢

转载自blog.csdn.net/xutian_curry/article/details/80430450