leetcode 121 买卖股票的最佳时机

简介

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

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;
    }
}
上一篇:解决win10系统alt+tab无法切换(或者需要切换2次)excel2007问题


下一篇:遇到GET http://localhost:8080/myweb/WEB-INF/js/jquery-3.4.1.js net::ERR_ABORTED 404