leetcode 121 Best Time to Buy and Sell Stock C++

来源:互联网 发布:plc编程实例 编辑:程序博客网 时间:2024/05/16 07:33

动态规划


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


0 0