From 23d4c4597c3359afd93d28f398836c82877cfcba Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 13 Dec 2022 10:54:08 +0800 Subject: [PATCH 1/3] =?UTF-8?q?update=200376.=E6=91=86=E5=8A=A8=E5=BA=8F?= =?UTF-8?q?=E5=88=97:=20=E4=BF=AE=E6=94=B9=E4=B8=80=E5=A4=84markdown?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index f9d3f97f..723d1d9a 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -286,7 +286,7 @@ class Solution: ### Go **贪心** -```golang +```go func wiggleMaxLength(nums []int) int { n := len(nums) if n < 2 { @@ -309,7 +309,7 @@ func wiggleMaxLength(nums []int) int { ``` **动态规划** -```golang +```go func wiggleMaxLength(nums []int) int { n := len(nums) if n <= 1 { From 55d722d9c087e3b9891e6f4324e6b27b0549b0a9 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 13 Dec 2022 15:01:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?update=200122.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BAII?= =?UTF-8?q?=EF=BC=9A=E4=BF=AE=E6=94=B9=20go=20=E4=BB=A3=E7=A0=81=20?= =?UTF-8?q?=E5=92=8C=20=E9=94=99=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0122.买卖股票的最佳时机II.md | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 57b174d9..d094da48 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -46,7 +46,7 @@ ### 贪心算法 -这道题目可能我们只会想,选一个低的买入,在选个高的卖,在选一个低的买入.....循环反复。 +这道题目可能我们只会想,选一个低的买入,再选个高的卖,再选一个低的买入.....循环反复。 **如果想到其实最终利润是可以分解的,那么本题就很容易了!** @@ -198,38 +198,40 @@ class Solution: ### Go: -```golang -//贪心算法 +贪心算法 +```go func maxProfit(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] + if prices[i] - prices[i-1] > 0 { + sum += prices[i] - prices[i-1] } } return sum } ``` - -```golang -//确定售卖点 +动态规划 +```go func maxProfit(prices []int) int { - var result,buy int - prices=append(prices,0)//在price末尾加个0,防止price一直递增 - /** - 思路:检查后一个元素是否大于当前元素,如果小于,则表明这是一个售卖点,当前元素的值减去购买时候的值 - 如果不小于,说明后面有更好的售卖点, - **/ - for i:=0;iprices[i+1]{ - result+=prices[i]-prices[buy] - buy=i+1 - }else if prices[buy]>prices[i]{//更改最低购买点 - buy=i - } + dp := make([][]int, len(prices)) + for i := 0; i < len(dp); i++ { + dp[i] = make([]int, 2) } - return result + // dp[i][0]表示在状态i不持有股票的现金,dp[i][1]为持有股票的现金 + dp[0][0], dp[0][1] = 0, -prices[0] + for i := 1; i < len(prices); i++ { + dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i]) + dp[i][1] = max(dp[i-1][0] - prices[i], dp[i-1][1]) + } + return dp[len(prices)-1][0] + +} +func max(a, b int) int { + if a > b { + return a + } + return b } ``` From c13f53e47e5d9a5247070c0f11a26f01b9f0c053 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Wed, 14 Dec 2022 14:21:23 +0800 Subject: [PATCH 3/3] =?UTF-8?q?update=200055.=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=EF=BC=9A=E4=BF=AE=E6=94=B9=E9=94=99=E5=AD=97=EF=BC=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=86=97=E4=BD=99=20go=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0055.跳跃游戏.md | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index 80c35c03..7584e952 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -72,13 +72,13 @@ public: ``` ## 总结 -这道题目关键点在于:不用拘泥于每次究竟跳跳几步,而是看覆盖范围,覆盖范围内一定是可以跳过来的,不用管是怎么跳的。 +这道题目关键点在于:不用拘泥于每次究竟跳几步,而是看覆盖范围,覆盖范围内一定是可以跳过来的,不用管是怎么跳的。 大家可以看出思路想出来了,代码还是非常简单的。 一些同学可能感觉,我在讲贪心系列的时候,题目和题目之间貌似没有什么联系? -**是真的就是没什么联系,因为贪心无套路!**没有个整体的贪心框架解决一些列问题,只能是接触各种类型的题目锻炼自己的贪心思维! +**是真的就是没什么联系,因为贪心无套路!**没有个整体的贪心框架解决一系列问题,只能是接触各种类型的题目锻炼自己的贪心思维! ## 其他语言版本 @@ -133,24 +133,6 @@ class Solution: ``` ### Go -```Go -func canJUmp(nums []int) bool { - if len(nums)<=1{ - return true - } - dp:=make([]bool,len(nums)) - dp[0]=true - for i:=1;i=0;j--{ - if dp[j]&&nums[j]+j>=i{ - dp[i]=true - break - } - } - } - return dp[len(nums)-1] -} -``` ```go // 贪心