LeetCode-Best Time to Buy and Sell Stock

来源:互联网 发布:ubuntu cp复制文件夹 编辑:程序博客网 时间:2024/06/05 19:03
class Solution {public:    int maxProfit(vector<int> &prices) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        int maxPrft = 0;        if (prices.size() > 1)        {            int maxRight = prices[prices.size() - 1];            maxPrft = max(0, maxRight - prices[0]);            for (int i = prices.size() - 2; i > 0; --i)            {                if (prices[i] < prices[0])                    maxPrft = max(maxPrft, maxRight - prices[i]);                if (prices[i] > maxRight)                {                    maxRight = prices[i];                    maxPrft = max(maxPrft, maxRight - prices[0]);                }                                }        }        return maxPrft;    }};

原创粉丝点击