简介

使用感觉类似动态规划的思路进行计算

code

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int inf = 1e9;
        int minPrice = inf;
        int maxProfit = 0;
        for(auto it :prices) {
            maxProfit = max(maxProfit, it - minPrice);
            minPrice = min(minPrice, it);
        }
        return maxProfit;
    }
};
class Solution {
    public int maxProfit(int[] prices) {
        int inf = 1000000000;
        int minPrice = inf, maxProfit = 0;
        for(int it : prices) {
            maxProfit = Math.max(maxProfit, it - minPrice);
            minPrice = Math.min(minPrice, it);
        }
        return maxProfit;
    }
}
posted on 2021-05-16 19:37  HDU李少帅  阅读(36)  评论(0编辑  收藏  举报