File tree Expand file tree Collapse file tree 4 files changed +39
-3
lines changed
1000-1099/1024.Video Stitching
1300-1399/1326.Minimum Number of Taps to Open to Water a Garden Expand file tree Collapse file tree 4 files changed +39
-3
lines changed Original file line number Diff line number Diff line change 49
49
50
50
** 方法二:贪心**
51
51
52
+ 我们可以用变量 ` mx ` 记录当前位置能够到达的最远位置,用变量 ` end ` 记录上一次跳跃的位置,用变量 ` steps ` 记录跳跃的次数。
53
+
54
+ 接下来,我们从 $0$ 开始枚举所有位置,用 $i+nums[ i] $ 来更新 ` mx ` ,当 $i=end$ 时,我们就需要进行一次跳跃,此时我们将 ` end ` 更新为 ` mx ` ,并将 ` steps ` 加 $1$。
55
+
56
+ 遍历结束,返回 ` steps ` 即可。
57
+
58
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。
59
+
60
+ 相似题目:
61
+
62
+ - [ 55. 跳跃游戏] ( /solution/0000-0099/0055.Jump%20Game/README.md )
63
+ - [ 1024. 视频拼接] ( /solution/1000-1099/1024.Video%20Stitching/README.md )
64
+ - [ 1326. 灌溉花园的最少水龙头数目] ( /solution/1300-1399/1326.Minimum%20Number%20of%20Taps%20to%20Open%20to%20Water%20a%20Garden/README.md )
65
+
52
66
<!-- tabs:start -->
53
67
54
68
### ** Python3**
Original file line number Diff line number Diff line change 43
43
44
44
<!-- 这里可写通用的实现逻辑 -->
45
45
46
- 贪心。
46
+ ** 方法一:贪心**
47
+
48
+ 我们用变量 ` mx ` 维护当前能够到达的最远下标,初始时 ` mx = 0 ` 。
49
+
50
+ 我们从左到右遍历数组,对于遍历到的每个位置 $i$,如果 $i\gt mx$,说明当前位置无法到达,直接返回 ` false ` 。否则,我们可以通过跳跃从位置 $i$ 到达的最远位置为 $i+nums[ i] $,我们用 $i+nums[ i] $ 更新 ` mx ` 的值,即 $mx=\max(mx,i+nums[ i] )$。
51
+
52
+ 遍历结束,直接返回 ` true ` 。
53
+
54
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。
55
+
56
+ 相似题目:
57
+
58
+ - [ 45. 跳跃游戏 II] ( /solution/0000-0099/0045.Jump%20Game%20II/README.md )
59
+ - [ 1024. 视频拼接] ( /solution/1000-1099/1024.Video%20Stitching/README.md )
60
+ - [ 1326. 灌溉花园的最少水龙头数目] ( /solution/1300-1399/1326.Minimum%20Number%20of%20Taps%20to%20Open%20to%20Water%20a%20Garden/README.md )
47
61
48
62
<!-- tabs:start -->
49
63
Original file line number Diff line number Diff line change 80
80
81
81
时间复杂度 $O(n+m)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别是数组 ` clips ` 的长度和 ` time ` 的值。
82
82
83
- 相似题目:[ 1326. 灌溉花园的最少水龙头数目] ( /solution/1300-1399/1326.Minimum%20Number%20of%20Taps%20to%20Open%20to%20Water%20a%20Garden/README.md )
83
+ 相似题目:
84
+
85
+ - [ 45. 跳跃游戏 II] ( /solution/0000-0099/0045.Jump%20Game%20II/README.md )
86
+ - [ 55. 跳跃游戏] ( /solution/0000-0099/0055.Jump%20Game/README.md )
87
+ - [ 1326. 灌溉花园的最少水龙头数目] ( /solution/1300-1399/1326.Minimum%20Number%20of%20Taps%20to%20Open%20to%20Water%20a%20Garden/README.md )
84
88
85
89
<!-- tabs:start -->
86
90
Original file line number Diff line number Diff line change 71
71
72
72
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为花园的长度。
73
73
74
- 相似题目:[ 1024. 视频拼接] ( /solution/1000-1099/1024.Video%20Stitching/README.md )
74
+ 相似题目:
75
+
76
+ - [ 45. 跳跃游戏 II] ( /solution/0000-0099/0045.Jump%20Game%20II/README.md )
77
+ - [ 55. 跳跃游戏] ( /solution/0000-0099/0055.Jump%20Game/README.md )
78
+ - [ 1024. 视频拼接] ( /solution/1000-1099/1024.Video%20Stitching/README.md )
75
79
76
80
<!-- tabs:start -->
77
81
You can’t perform that action at this time.
0 commit comments