Leetcode | Jump Game I && II

Jump Game I


Given an array of non-negative integers, you are initially positioned at the
first index of the array.

Each element in the array represents your maximum jump length at that
position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

Method I

动态规划。reachable[i]存的是能不能从i跳到n-1。

如果存在1<=j<=A[i],使得reachable[i+j]=true,那么reachable[i]=true;否则reachable[i]=false;

但是这样穷搜的话会TLE。所以需要继续避免一些计算。

如果A[i]<=A[i+1]+1时,只要A[i]不为0,那么i必定能够跳到i+1,i能够跳到的位置,i+1都能跳到,所以reachable[i]=reachable[i+1];

如果A[i]>A[i+1]+1时,因为从i往后A[i +
1]+1都已经在计算reachable[i+1]的时候计算过了。如果此时reachable[i+1]=false,我们就从i往后A[i+1]+2开始跳。


 1 class Solution {
2 public:
3 bool canJump(int A[], int n) {
4 if (n == 0) return true;
5
6 vector<bool> reachable(n, false);
7 reachable[n - 1] = true;
8
9 for (int i = n - 2; i >= 0; --i) {
10 if (A[i] == 0) continue;
11 if (n - 1 - i <= A[i]) {
12 reachable[i] = true;
13 } else {
14 reachable[i] = reachable[i + 1];
15 if (reachable[i]) continue;
16 for (int j = A[i + 1] + 2; j <= A[i]; ++j) {
17 if (reachable[i + j]) {
18 reachable[i] = true;
19 break;
20 }
21 }
22 }
23 }
24
25 return reachable[0];
26 }
27 };

Method II

参考了水中的鱼的算法,
更简单。算出一个最大的覆盖区间maxcover,如果i在这区间里,用它能跳到的最远位置来更新maxcover。

当然你也可以把它理解为动态规划的一种。Code
Granker
总结的”局部最优和全局最优解法“的一种。


 1 class Solution {
2 public:
3 bool canJump(int A[], int n) {
4 int maxcover = 0;
5
6 for (int i = 0; i < n && i <= maxcover; ++i) {
7 if (A[i] + i > maxcover) maxcover = A[i] + i;
8 if (maxcover >= n - 1) return true;
9 }
10 return false;
11 }
12 };

Jump Game II


Given an array of non-negative integers, you are initially positioned at the
first index of the array.

Each element in the array represents your maximum jump length at that
position.

Your goal is to reach the last index in the minimum number of jumps.

For example:
Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2.
(Jump 1 step from index 0 to 1,
then 3 steps to the last index.)

Method I

同上,但是要分的情况更多了。76ms accept.

min[i]就是从i跳到n-1的最小步数;

1. A[i] == 0,不可达;

2. n-1-i <= A[i],一步可达;

3. A[i] == 1,min[i] = min[i +1]+1; 先跳到i+1,再从i+1往向跳;

4. A[i] == A[i+1] +1,那么从i+1可以跳到的位置,i也可以一步跳到,所以min[i] = min[i+1];

5. 剩下的情况,就要尝试i能跳到的范围,求最小步数了;


 1 class Solution {
2 public:
3 int jump(int A[], int n) {
4 if (n == 0) return 0;
5
6 vector<int> min(n, n + 1);
7 min[n - 1] = 0;
8
9 for (int i = n - 2; i >= 0; --i) {
10 if (A[i] == 0) continue;
11 if (n - 1 - i <= A[i]) {
12 min[i] = 1;
13 } else if (A[i] == 1) {
14 min[i] = min[i + 1] + 1;
15 } else if (A[i] == A[i + 1] + 1) {
16 min[i] = min[i + 1];
17 }else {
18 int m = min[i];
19 for (int j = 1; j <= A[i]; ++j) {
20 if (min[i + j] + 1 < m) {
21 m = min[i + j] + 1;
22 }
23 }
24 min[i] = m;
25 }
26 }
27
28 return min[0];
29 }
30 };

Method II

水中的鱼的解法。维护一个最大覆盖区间,计算覆盖区间的个数就是最小步数。


 1 class Solution {
2 public:
3 int jump(int A[], int n) {
4 if (n <= 1) return 0;
5 int s = 0, e = 0, count = 0;
6
7 while (s <= e && e < n) {
8 count++;
9 int max = 0;
10 for (int i = s; i <= e; ++i) {
11 if (A[i] + i > max) max = A[i] + i;
12 if (max >= n - 1) return count;
13 }
14 s = e + 1;
15 e = max;
16 }
17 return 0;
18 }
19 };

Leetcode | Jump Game I && II,布布扣,bubuko.com

时间: 2024-10-06 03:08:22

Leetcode | Jump Game I && II的相关文章

[LeetCode] Jump Game II(贪婪算法)

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

LeetCode: Jump Game II [044]

Perface 如果让你实现这个页面和一些操作的,比如点击1.2.3等就在那个input text中显示,还有删除功能,拨打我们先不要管它,只是模拟而已.要是我刚开始做的话,我会这样做: 用css.HTML布局那个界面 用javascript的事件委托监听那个按钮的父节点的点击事件 但是如果我想用面向对象的思想做呢?我是用Ext做的,所以我想说的是它帮我封装了很多.可能一些没用过Ext的人不太了解我下面贴的代码,但是我会尽量解释清楚的! Description ContactTelPanel =

[leetcode]Jump Game II @ Python

原题地址:https://oj.leetcode.com/problems/jump-game-ii/ 题意: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal i

Leetcode:Jump Game 跳跃楼梯

Jump Game: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For e

LeetCode: Jump Game [054]

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one