File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -356,6 +356,7 @@ func canJump(nums []int) bool {
356356> 你的目标是使用最少的跳跃次数到达数组的最后一个位置。
357357
358358``` go
359+ // v1动态规划(其他语言超时参考v2)
359360func jump (nums []int ) int {
360361 // 状态:f[i] 表示从起点到当前位置最小次数
361362 // 推导:f[i] = f[j],a[j]+j >=i,min(f[j]+1)
@@ -383,6 +384,26 @@ func min(a, b int) int {
383384}
384385```
385386
387+ ``` go
388+ // v2 动态规划+贪心优化
389+ func jump (nums []int ) int {
390+ n := len (nums)
391+ f := make ([]int , n)
392+ f[0 ] = 0
393+ for i := 1 ; i < n; i++ {
394+ // 取第一个能跳到当前位置的点即可
395+ // 因为跳跃次数的结果集是单调递增的,所以贪心思路是正确的
396+ idx := 0
397+ for idx<n&&idx+nums[idx]<i{
398+ idx++
399+ }
400+ f[i]=f[idx]+1
401+ }
402+ return f[n-1 ]
403+ }
404+
405+ ```
406+
386407### [ palindrome-partitioning-ii] ( https://leetcode-cn.com/problems/palindrome-partitioning-ii/ )
387408
388409> 给定一个字符串 _ s_ ,将 _ s_ 分割成一些子串,使每个子串都是回文串。
You can’t perform that action at this time.
0 commit comments