leetcode--121. Best Time to Buy and Sell Stock

来源:互联网 发布:人工智能 李开复 pdf 编辑:程序博客网 时间:2024/06/06 10:53

地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/description

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


0 0
原创粉丝点击