Skip to content

Commit b70881e

Browse files
committed
fix jump-game-ii
1 parent 4056779 commit b70881e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

basic_algorithm/dp.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ func canJump(nums []int) bool {
356356
> 你的目标是使用最少的跳跃次数到达数组的最后一个位置。
357357
358358
```go
359+
// v1动态规划(其他语言超时参考v2)
359360
func 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_ 分割成一些子串,使每个子串都是回文串。

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy