Sunday, April 5, 2020

LeetCode 122. Best Time to Buy and Sell Stock II

1.Problem
https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3287/

2.Idea
Just keep adding positive diffs.

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

No comments:

Post a Comment