题目

一只股票在某些时间节点的价格为{9,11,8,5,7,12,16,14}。如果我们能在价格为 5 的时候买入并在价格为 16 时卖出,则能获得最大的利润为 11.

解题思路

  1. 要先买入才能卖出,先找最低价格点
  2. 再找最低价格之后的最高价格,用 maxProfit 表示最大利润
public int maxProfit(int[] nums) {
    if (nums == null || nums.length == 0) return 0;

    int min = Integer.MAX_VALUE;
    int maxProfit = 0;
    for (int i = 0; i < nums.length; i++) {
        min = Math.min(min, nums[i]);

        maxProfit = Math.max(maxProfit, nums[i] - min);
    }

    return maxProfit;
}

results matching ""

    No results matching ""