LeetCode_Best Time to Buy and Sell Stock II

class Solution {

public:

    int maxProfit(vector<int> &prices) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        if( prices.size() < 2 ) return 0;

        int result = 0;

        int min = 0,i;

        for(i = 1; i< prices.size(); i++)

        {

            if(prices[i] >= prices[i-1]) 

                continue;

            else{

            

               result += prices[i-1] - prices[min];

               min = i;

            

            }

        

        }

        if(prices[i-1] >prices[min])

           result += prices[i-1] -prices[min];

           

        return result;

    }

};

 

你可能感兴趣的:(LeetCode)