lee122买卖股票的最佳时机 I、II golang

买卖股票的最佳时机 I

/*
Question description:

给定一个数组,它的第 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。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/

方法一:Brute Force

分别以每一天的都买进, 然后求最大值, 再在所有天数买进的最大值
// 时间复杂度为O(n^2),空间复杂度为O(1)

func bruteForces(prices []int) int {
	if prices == nil || len(prices) == 0 {
		return 0
	}
	maxProfit := 0
	for i := 0; i < len(prices); i++ {
		for j := i + 1; j < len(prices); j++ {
			if prices[j] > prices[i] && prices[j]-prices[i] > maxProfit {
				maxProfit = prices[j] - prices[i]
			}
		}
	}
	return maxProfit
}
方法二:动态规划

因为只能购买一次,我们可以理解为当前的卖出,其实就是减去现在卖出之前的买入最小值。 之后求出所有的可能的最大值。
// 时间复杂度为O(n),空间复杂度为O(1)

func DynamicProgramme(prices []int) int {
	if prices == nil || len(prices) == 0 {
		return 0
	}

	maxProfit, min := 0, math.MaxInt32
	for i := 0; i < len(prices); i++ {
		if prices[i] < min {
			min = prices[i]
		} else if prices[i]-min > maxProfit {
			maxProfit = prices[i] - min
		}

	}
	return maxProfit
}

买卖股票的最佳时机 II

/**
question:
给定一个数组,它的第 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

  • Author:sxy
    */
方法一:Brute Force

因为不限制购买次数,所以会搞出来所有的可能
所以呢, 我们如何得到当前节点买入的最大收入呢?
可以采用回溯思想,即就是
当前节点的最大收入=后一个节点的最大收入+当前节点的收入;
退出条件呢,其实就是在最后一个节点的时候

func BruteForce(prices []int) int {
	res := bruteForce(prices, 0)
	return res
}

func bruteForce(prices []int, n int) int {
	if n >= len(prices) {
		return 0
	}

	max := 0
	for i := n; i < len(prices); i++ {
		maxProfit := 0
		for j := i + 1; j < len(prices); j++ {
			profit := bruteForce(prices, j+1) + prices[j] - prices[i]
			if profit > maxProfit {
				maxProfit = profit
			}
		}

		if max < maxProfit {
			max = maxProfit
		}
	}
	return max
}
方法二:贪心算法

greed、 没有对买入的次数做限制,所以我们可以将此问题看做,
将股票从第i天买入,到第j天卖出 等于 将股票第i天买入,第i+p天卖出,又买进,第j天又卖出是一样的。
// 当低价时,我们就买进,只要下次比买进的高,我就直接卖出,然后又买进。

func MaxProfit(prices []int) int {
	if prices == nil && len(prices) == 0 {
		return 0
	}
	profit := 0
	for i := 1; i < len(prices); i++ {
		if prices[i] > prices[i-1] {
			profit += prices[i] - prices[i-1]
		}
	}
	return profit
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值