Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb70881e

Browse files
committed
fix jump-game-ii
1 parent4056779 commitb70881e

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
funcjump(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+
funcjump(nums []int)int {
390+
n:=len(nums)
391+
f:=make([]int, n)
392+
f[0] =0
393+
fori:=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)

[8]ページ先頭

©2009-2025 Movatter.jp