mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
update 0121.买卖股票的最佳时机:优化代码风格
This commit is contained in:
@ -89,7 +89,7 @@ dp[i][1] 表示第i天不持有股票所得最多现金
|
||||
|
||||
**注意这里说的是“持有”,“持有”不代表就是当天“买入”!也有可能是昨天就买入了,今天保持持有的状态**
|
||||
|
||||
很多同学把“持有”和“买入”没分区分清楚。
|
||||
很多同学把“持有”和“买入”没区分清楚。
|
||||
|
||||
在下面递推公式分析中,我会进一步讲解。
|
||||
|
||||
@ -103,11 +103,11 @@ dp[i][1] 表示第i天不持有股票所得最多现金
|
||||
|
||||
如果第i天不持有股票即dp[i][1], 也可以由两个状态推出来
|
||||
* 第i-1天就不持有股票,那么就保持现状,所得现金就是昨天不持有股票的所得现金 即:dp[i - 1][1]
|
||||
* 第i天卖出股票,所得现金就是按照今天股票佳价格卖出后所得现金即:prices[i] + dp[i - 1][0]
|
||||
* 第i天卖出股票,所得现金就是按照今天股票价格卖出后所得现金即:prices[i] + dp[i - 1][0]
|
||||
|
||||
同样dp[i][1]取最大的,dp[i][1] = max(dp[i - 1][1], prices[i] + dp[i - 1][0]);
|
||||
|
||||
这样递归公式我们就分析完了
|
||||
这样递推公式我们就分析完了
|
||||
|
||||
3. dp数组如何初始化
|
||||
|
||||
@ -121,7 +121,7 @@ dp[0][1]表示第0天不持有股票,不持有股票那么现金就是0,所
|
||||
|
||||
4. 确定遍历顺序
|
||||
|
||||
从递推公式可以看出dp[i]都是有dp[i - 1]推导出来的,那么一定是从前向后遍历。
|
||||
从递推公式可以看出dp[i]都是由dp[i - 1]推导出来的,那么一定是从前向后遍历。
|
||||
|
||||
5. 举例推导dp数组
|
||||
|
||||
@ -326,53 +326,40 @@ Go:
|
||||
> 贪心法:
|
||||
```Go
|
||||
func maxProfit(prices []int) int {
|
||||
low := math.MaxInt32
|
||||
rlt := 0
|
||||
for i := range prices{
|
||||
low = min(low, prices[i])
|
||||
rlt = max(rlt, prices[i]-low)
|
||||
min := prices[0]
|
||||
res := 0
|
||||
for i := 1; i < len(prices); i++ {
|
||||
if prices[i] - min > res {
|
||||
res = prices[i]-min
|
||||
}
|
||||
if min > prices[i] {
|
||||
min = prices[i]
|
||||
}
|
||||
}
|
||||
|
||||
return rlt
|
||||
}
|
||||
func min(a, b int) int {
|
||||
if a < b{
|
||||
return a
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b{
|
||||
return a
|
||||
}
|
||||
|
||||
return b
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
> 动态规划:版本一
|
||||
```Go
|
||||
func maxProfit(prices []int) int {
|
||||
length:=len(prices)
|
||||
if length==0{return 0}
|
||||
dp:=make([][]int,length)
|
||||
for i:=0;i<length;i++{
|
||||
dp[i]=make([]int,2)
|
||||
length := len(prices)
|
||||
if length == 0{return 0}
|
||||
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],-prices[i])
|
||||
dp[i][1]=max(dp[i-1][1],dp[i-1][0]+prices[i])
|
||||
dp[0][0] = -prices[0]
|
||||
dp[0][1] = 0
|
||||
for i := 1; i < length; i++ {
|
||||
dp[i][0] = max(dp[i-1][0], -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{
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
@ -385,7 +372,7 @@ func maxProfit(prices []int) int {
|
||||
dp := [2][2]int{}
|
||||
dp[0][0] = -prices[0]
|
||||
dp[0][1] = 0
|
||||
for i := 1; i < len(prices); i++{
|
||||
for i := 1; i < len(prices); i++ {
|
||||
dp[i%2][0] = max(dp[(i-1)%2][0], -prices[i])
|
||||
dp[i%2][1] = max(dp[(i-1)%2][1], dp[(i-1)%2][0]+prices[i])
|
||||
}
|
||||
|
Reference in New Issue
Block a user