最后的结果
LeetCode 121. Best Time to Buy and Sell Stock
开始的时候是算一个矩阵,当然就会很慢
LeetCode 121. Best Time to Buy and Sell Stock
然后想着能不能一遍算完,自己想了两个指针,总是有某些情况不行
最后看了答案
LeetCode 121. Best Time to Buy and Sell Stock
由于不能回头,如果这个价格低于我的买入,就当作买入,如果高于,就看利润是不是比之前算的大
我的代码

int maxProfit(int* prices, int pricesSize) {
    if (pricesSize == 1 || pricesSize == 0) {
        return 0;
    }
    int max = 0;
    int in = prices[0];
    for (int i = 1; i < pricesSize; ++i) {
        int t = prices[i];
        if (t < in) {
            in = t;
        } else {
            if(t - in > max) {
                max = t - in;
            }
        }
    }
    return max;
}

相关文章:

  • 2022-02-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
猜你喜欢
  • 2021-06-27
  • 2021-11-23
  • 2021-09-01
  • 2021-10-01
  • 2021-06-08
  • 2021-09-08
相关资源
相似解决方案