zoukankan      html  css  js  c++  java
  • 0122买卖股票的最佳时机II Marathon

    给定一个数组 prices ,其中 prices[i] 是一支给定股票第 i 天的价格。

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

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

    示例 1:

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

    示例 2:

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

    示例 3:

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

    提示:

    1 <= prices.length <= 3 * 104
    0 <= prices[i] <= 104

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii

    思考:

    python

    # 0122.买卖股票最佳时机II
    
    
    class Solution:
        def maxprofit(self, prices: [int]) -> int:
            """
            贪心算法,低的买入,高的卖出,累加低买高卖
            累加:prices[m]-prices[n]=prices[m]-prices[m-1] + ... + prices[n+1]-prices[n]
            所以只要累加正差即可
            e.g. prices -> [7,1,5,10,3,6,4]
                 profit ->   [-6,4,5,-7,3,-2]
                 profits = 4+5+3=12
            :param prices:
            :return:
            """
            res = 0
            # 第二天开始卖出
            for i in range(1, len(prices)):
                res += max(prices[i]-prices[i-1], 0)
            return res
    
    # 动态规划
    # 0122.买卖股票最佳时机II
    
    class Solution:
        def maxProfit(self, prices: [int]) -> int:
            """
            动态规划-股票问题, 时间O(n),空间O(n)
    
            dp数组的含义:
            dp[i][0] 表示第i天持有股票所得现金。
            dp[i][1] 表示第i天不持有股票所得最多现金
    
            第i天持有股票即dp[i][0], 那么可以由两个状态推出来
            第i-1天就持有股票,那么就保持现状,所得现金就是昨天持有股票的所得现金 即:dp[i - 1][0]
            第i天买入股票,所得现金就是昨天不持有股票的所得现金减去 今天的股票价格 即:dp[i - 1][1] - prices[i]
    
            第i天不持有股票即dp[i][1]的情况, 依然可以由两个状态推出来
            第i-1天就不持有股票,那么就保持现状,所得现金就是昨天不持有股票的所得现金 即:dp[i - 1][1]
            第i天卖出股票,所得现金就是按照今天股票佳价格卖出后所得现金即:prices[i] + dp[i - 1][0]
    
            :param prices:
            :return:
            """
            length = len(prices)
            dp = [[0]*2 for _ in range(length)]
            dp[0][0] = -prices[0]
            dp[0][1] = 0
            for i in range(1, length):
                dp[i][0] = max(dp[i-1][0], dp[i-1][1]-prices[i])
                dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i])
            return dp[-1][1]
    
        def maxProfit2(self, prices: [int]) -> int:
            """
            动态规划-股票问题,滚动数组,只需要2*2数组
            :param prices:
            :return:
            """
            length = len(prices)
            dp = [[0]*2 for _ in range(2)]
            dp[0][0] = -prices[0]
            dp[0][1] = 0
            for i in range(1, length):
                dp[i%2][0] = max(dp[(i-1)%2][0], dp[(i-1)%2][1]-prices[i])
                dp[i%2][1] = max(dp[(i-1)%2][1], dp[(i-1)%2][0]+prices[i])
            return dp[(length-1)%2][1]
    
    

    golang

    package greedy
    
    // 贪心算法
    func maxProfitII(prices []int) int {
    	var sum int
    	for i:=1;i<len(prices);i++ {
    		// 累加每次大于0的交易
    		if prices[i] - prices[i-1] > 0 {
    			sum += prices[i] - prices[i-1]
    		}
    	}
    	return sum
    }
    
    // 动态规划
    // 动态规划
    func maxProfit2(prices []int) int {
    	length := len(prices)
    	dp := make([][]int, length)
    	for i:=0;i<length;i++ {
    		dp[i] = make([]int, 2)
    	}
    	dp[0][0] = -prices[0]
    	dp[0][1] = 0
    	for i:=1;i<length;i++ {
    		dp[i][0] = max(dp[i-1][0], dp[i-1][1]-prices[i])
    		dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i])
    	}
    	return dp[length-1][1]
    }
    
    func max(a, b int) int {
    	if a > b {return a}
    	return b
    }
    
    
  • 相关阅读:
    Adjacent Bit Counts(uvalive)
    UVALIVE 4556 The Next Permutation
    vector(实现存图)
    最大连续子序列和(模板)
    全选和反选
    .netCore上传图片,要用FormFileCollection ,不能用List
    .NET-Core中 HttpContext.Response.Write() 中文输出乱码
    Core中Cookie和Session的新用法
    Ajax反填
    复选框变成单选
  • 原文地址:https://www.cnblogs.com/davis12/p/15600804.html
Copyright © 2011-2022 走看看