[leetCode]122.买卖股票的最佳时机II

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

题目

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

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

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入: [7,1,5,3,6,4]
输出: 7
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
示例 2:

输入: [1,2,3,4,5]
输出: 4
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
     因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
示例 3:

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

提示:

1 <= prices.length <= 3 * 10 ^ 4
0 <= prices[i] <= 10 ^ 4

一次遍历

下面是自己写的一次遍历,思路是每次找到波谷min后开始爬坡,如果没有到转折点就将这次利润和上次利润进行比较,如果大于上次利润累加器sum就加上这次利润减去上次利润将利润最大化

class Solution {
    public int maxProfit(int[] prices) {
        int min = 0;
        int prfit = 0;
        int sum = 0;
        for(int i = 1; i < prices.length; i++){
            if(prices[i] < prices[i-1]) {//更新最小值到达谷底
                min = i;
                prfit = 0;
            }else if(prices[i] > prices[min] && prices[i] > prices[i-1]){//在上升时不断更新prift
                int temp = prfit;//上次利润
                prfit = prices[i] - prices[min];//当前利润
                if(prfit > temp){//如果当前利润比上次利润多则加上这次利润,减去上次利润
                     sum = sum + prfit - temp;
                }
            }
        }
        return sum;
    }
}

贪心算法

下面是官方的一次遍历,每次爬坡将一小段的利润累加,使得利润最大化

class Solution {
    public int maxProfit(int[] prices) {
        int maxProfit = 0;
        for(int i = 1; i < prices.length; i++){
           if(prices[i] > prices[i-1]){
               maxProfit += prices[i] - prices[i-1];
           }
        }
        return maxProfit;
    }
}

峰谷法

寻找曲线的每个波谷和波峰,计算之间的利润,将所有波峰波谷之间的利润求和

class Solution {
    public int maxProfit(int[] prices) {
        int i = 0;
        int valley = prices[0];//波谷
        int peak = prices[0];//波峰
        int maxProfit = 0;
        while(i < prices.length -1){
            //从波峰开始寻找波谷
            while(i < prices.length - 1 && prices[i] >= prices[i+1])
                ++i;
            valley = prices[i];
            //从波谷开始攀爬寻找波峰
            while( i < prices.length - 1 && prices[i] <= prices[i + 1])
                ++i;
            peak = prices[i];
            maxProfit += peak - valley;
        }
        return maxProfit;
    }
}

动态规化

此题与121题的不同在于,第121题只能买卖一次股票,而此题可以尽可能多的完成交易,状态的定义还是一样,但是dp[i][1]的转移方程发生了变化:
d [ i ] [ 1 ] = M a x ( d p [ i − 1 ] [ 1 ] , d p [ i − 1 ] [ 0 ] − p r i c e s [ i ] ) d[i][1] = Max(dp[i-1][1] ,dp[i-1][0]-prices[i]) d[i][1]=Max(dp[i1][1],dp[i1][0]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;
        dp[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], dp[i - 1][0] - prices[i]);// 无限次买入
        }
        return dp[n - 1][0];
    }
}

空间优化:

class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        if (n == 0) return 0;
        int p0 = 0;
        int p1 = -prices[0];
        for (int i = 1; i < n; i++) {
            p0 = Math.max(p0, p1 + prices[i]);
            p1 = Math.max(p1, p0 - prices[i]);// 无限次买入
        }
        return p0;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值