[leetCode]121.买股票的最佳时机

42 篇文章 0 订阅
31 篇文章 0 订阅

题目

链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。

注意:你不能在买入股票前卖出股票。

示例 1:

输入: [7,1,5,3,6,4]
输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
示例 2:

输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。

解法

思路:先查找整个区间的最大峰值,再查找该区间之前的最小值,计算利润,
然后缩小区间,查找峰值与最小值,再次计算并更新利润。

class Solution {
    public int maxProfit(int[] prices) {
       int lo = 0, hi = prices.length - 1;
       int profit = 0;
       while(hi > lo){
           int max = lo;
           for(int i = lo; i <= hi; i++){//查找整个区间最大值
               if(prices[i] > prices[max]) max = i;
           }
           int min = lo;
           for(int i = lo; i < max; i++){//查找当前区间最大值之前的最小值
               if(prices[i] < prices[min]) min = i;
           }
           if(prices[max] - prices[min] > profit){//计算差值进行更新
               profit = prices[max] - prices[min];
           }
           lo = max + 1;//缩小区间范围继续
       }
       return profit;
    }
}

贪心算法

使用一个变量记录历史股票的最小值,如果当前值小于最小值则更新历史最小值,如果当前值大于历史最小值则计算能赚多少钱,然后取最大即可

class Solution {
    public int maxProfit(int[] prices) {
        int historyMin = Integer.MAX_VALUE;
        int maxP = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < historyMin) {
                historyMin = prices[i];
            } else {
                maxP = Math.max(maxP, prices[i] - historyMin);
            }
        }
        return maxP;
    }
}

动态规化

此题只能买卖股票一次,可以dp[i]为第i天能获得的最大收益,每一天有两个状态

  • 不持股
  • 持有一张股票

所以使用dp[i][0]表示第i天不持股的状态,使用dp[i][1]表示第i天持有一张股票的状态

如果是dp[i][0],则前一天可能状态不变也是不持股,也可能前一天持股但卖掉了所以
d p [ i ] [ 0 ] = M a x ( d p [ i − 1 ] [ 0 ] , d p [ i − 1 ] [ 1 ] + p r i c e s [ i ] ) dp[i][0] = Max(dp[i-1][0], dp[i-1][1] + prices[i]) dp[i][0]=Max(dp[i1][0],dp[i1][1]+prices[i])

如果是dp[i][1],则前一天可能状态不变,也可能前一天不持股,但买了一张股票所以

d [ i ] [ 1 ] = M a x ( d p [ i − 1 ] [ 1 ] , − p r i c e s [ i ] ) d[i][1] = Max(dp[i-1][1] ,-prices[i]) d[i][1]=Max(dp[i1][1],prices[i])
只能买卖一次所以是-prices[i]

class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        if (n == 0) return 0;
        int[][] dp = new int[n][2];
        dp[0][0] = 0; // 第0持有0张股票, 还没有买,收益为0
        dp[0][1] = -prices[0];// 第0天持有1张股票, 买了一张收益为-prices[0]
        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],  - prices[i]);
        }
        return dp[n-1][0];
    }
}

由于dp[i]只与dp[i-1]有关,所以可以使用两个变量对空间进行优化

class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        if (n == 0) return 0;
        int p0 = 0; // 第0持有0张股票, 还没有买,收益为0
        int p1 = -prices[0];// 第0天持有1张股票, 买了一张收益为-prices[0]
        for (int i = 1; i < n; i++) {
            p0 = Math.max(p0, p1 + prices[i]);
            p1 = Math.max(p1,  -prices[i]);
        }
        return p0;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值