当前位置:编程学习 > C/C++ >>

[LeetCode] Best Time to Buy and Sell Stock II

[cpp]
class Solution { 
public: 
    int maxProfit(vector<int> &a) { 
        // Start typing your C/C++ solution below 
        // DO NOT write int main() function 
         
        int prevIdx = 0; 
        int res = 0; 
         
        for (int i = 1; i < a.size(); ++i) 
        { 
            if (a[i] > a[prevIdx]) 
            { 
                res += a[i] - a[prevIdx]; 
                 
            } 
            prevIdx = i; 
        } 
         
        return res; 
    } 
}; 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,