Golang每日一练(leetDay0104) 买卖股票最佳时机之含冷冻期、手续费-LMLPHP

目录

309. 最佳买卖股票时机含冷冻期 Best-time-to-buy-and-sell-stock-with-cooldown  🌟🌟

714. 买卖股票的最佳时机含手续费 Best-time-to-buy-and-sell-stock-with-transaction-fee  🌟🌟

🌟 每日一练刷题专栏 🌟

Rust每日一练 专栏

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


309. 最佳买卖股票时机含冷冻期 Best-time-to-buy-and-sell-stock-with-cooldown

给定一个整数数组prices,其中第  prices[i] 表示第 i 天的股票价格 。​

设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):

  • 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。

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

示例 1:

输入: prices = [1,2,3,0,2]
输出: 3 
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]

示例 2:

输入: prices = [1]
输出: 0

提示:

  • 1 <= prices.length <= 5000
  • 0 <= prices[i] <= 1000

 代码1:动态规划

package main

import "fmt"

func maxProfit(prices []int) int {
	if len(prices) == 0 {
		return 0
	}
	n := len(prices)
	dp := make([][3]int, n)
	dp[0][0], dp[0][1], dp[0][2] = -prices[0], 0, 0
	for i := 1; i < n; i++ {
		dp[i][0] = max(dp[i-1][0], dp[i-1][2]-prices[i])
		dp[i][1] = dp[i-1][0] + prices[i]
		dp[i][2] = max(dp[i-1][1], dp[i-1][2])
	}
	return max(dp[n-1][1], dp[n-1][2])
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	prices := []int{1, 2, 3, 0, 2}
	fmt.Println(maxProfit(prices))
	prices = []int{1}
	fmt.Println(maxProfit(prices))
}

 代码2:

package main

import "fmt"

func maxProfit(prices []int) int {
	if len(prices) == 0 {
		return 0
	}
	n := len(prices)
	hold, sold, rest := -prices[0], 0, 0
	for i := 1; i < n; i++ {
		preSold := sold
		sold = hold + prices[i]
		hold = max(hold, rest-prices[i])
		rest = max(rest, preSold)
	}
	return max(sold, rest)
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	prices := []int{1, 2, 3, 0, 2}
	fmt.Println(maxProfit(prices))
	prices = []int{1}
	fmt.Println(maxProfit(prices))
}

输出:

3
0


714. 买卖股票的最佳时机含手续费 Best-time-to-buy-and-sell-stock-with-transaction-fee

给定一个整数数组 prices,其中 prices[i]表示第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。

你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。

返回获得利润的最大值。

注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。

示例 1:

输入:prices = [1, 3, 2, 8, 4, 9], fee = 2
输出:8
解释:能够达到的最大利润:  
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8

示例 2:

输入:prices = [1,3,7,5,10,3], fee = 3
输出:6

提示:

  • 1 <= prices.length <= 5 * 10^4
  • 1 <= prices[i] < 5 * 10^4
  • 0 <= fee < 5 * 10^4

代码:贪心法

package main

import "fmt"

func maxProfit(prices []int, fee int) int {
	if len(prices) < 2 {
		return 0
	}
	buy, sell := -prices[0], 0
	for i := 1; i < len(prices); i++ {
		buy = max(buy, sell-prices[i])
		sell = max(sell, buy+prices[i]-fee)
	}
	return sell
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	prices := []int{1, 3, 2, 8, 4, 9}
	fee := 2
	fmt.Println(maxProfit(prices, fee))

	prices = []int{1, 3, 7, 5, 10, 3}
	fee = 3
	fmt.Println(maxProfit(prices, fee))
}

输出:

8
6


相关题目:

121. 买卖股票的最佳时机  🌟

122. 买卖股票的最佳时机 II  🌟🌟

123. 买卖股票的最佳时机 III  🌟🌟🌟

188. 买卖股票的最佳时机 IV  🌟🌟🌟


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/ 

06-22 08:54