美文网首页Leetcode
Leetcode 122. Best Time to Buy a

Leetcode 122. Best Time to Buy a

作者: SnailTyan | 来源:发表于2018-10-30 18:31 被阅读1次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Best Time to Buy and Sell Stock II

2. Solution

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size() == 0) {
            return 0;
        }
        int profit = 0;
        for(int i = 1; i < prices.size(); i++) {
            int diff = prices[i] - prices[i - 1];
            if( diff > 0) {
                profit += diff;
            }
        }
        return profit;
    }
};

Reference

  1. https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/

相关文章

网友评论

    本文标题:Leetcode 122. Best Time to Buy a

    本文链接:https://www.haomeiwen.com/subject/ypqntqtx.html