You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//Greedy. Within max range we can jump to, do another loop to renew the max we can go.
45
+
publicclassSolution {
46
+
publicintjump(int[] nums) {
47
+
if (nums == null || nums.length <= 1) {
48
+
return0;
49
+
}
50
+
intindex = 0;
51
+
intstep = 0;
52
+
intrange = 0;
53
+
intmaxRange = 0;
54
+
55
+
while (index < nums.length) {
56
+
if (range >= nums.length - 1) {
57
+
break;
58
+
}
59
+
while (index <= range) {
60
+
maxRange = Math.max(maxRange, index + nums[index]);
61
+
index++;
62
+
}
63
+
range = maxRange;
64
+
step++;
65
+
}
66
+
returnstep;
67
+
}
68
+
}
69
+
70
+
71
+
/*
72
+
16
73
Thanks to Yu’s Garden blog
17
74
Thinking process:
18
75
0. Use two pointers pStart and pEnd to track the potential locations we can move to.
19
76
Consider a range from current spot to the farthest spot: try to find a max value from this range, and see if the max can reach the tail of array.
20
77
If no max can read the tail of array, that means we need to move on. At this point, let pStart = pEnd + 1. At same time, move pEnd to the max spot we can go to. Since pEnd moves forward, we could step++
**132. [Jump Game II.java](https://github.com/shawnfan/LintCode/blob/master/Java/Jump Game II.java)**Given an array of non-negative integers, you are initially positioned at the first index of the array.
2451
-
2452
-
Each element in the array represents your maximum jump length at that position.
2453
+
**132. [Jump Game II.java](https://github.com/shawnfan/LintCode/blob/master/Java/Jump Game II.java)** Level: Hard
2453
2454
2454
-
Your goal is to reach the last index in the minimum number of jumps.
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.)
2457
+
维护一个range, 是最远我们能走的.
2460
2458
2461
-
Tags Expand
2462
-
Greedy Array
2459
+
index/i 是一步一步往前, 每次当 i <= range, 做一个while loop, 在其中找最远能到的地方 maxRange
2463
2460
2464
-
Thanks to Yu’s Garden blog
2465
-
Thinking process:
2466
-
0. Use two pointers pStart and pEnd to track the potential locations we can move to.
2467
-
Consider a range from current spot to the farthest spot: try to find a max value from this range, and see if the max can reach the tail of array.
2468
-
If no max can read the tail of array, that means we need to move on. At this point, let pStart = pEnd + 1. At same time, move pEnd to the max spot we can go to. Since pEnd moves forward, we could step++
2469
-
If max reach the tail of array, return the steps.
2470
-
*/
2461
+
然后更新 range = maxRange
2471
2462
2472
-
public class Solution {
2473
-
/**
2474
-
* @param A: A list of lists of integers
2475
-
* @return: An integer
2476
-
*/
2477
-
public int jump(int[] A) {
2478
-
if (A == null || A.length == 0) {
2479
-
return 0;
2480
-
}
2481
-
int pStart = 0;
2482
-
int pEnd = 0;
2483
-
int steps = 0;
2484
-
while (pEnd < A.length - 1) {
2485
-
steps++; //Cound step everytime when pEnd is moving to the farthest.
2486
-
int farthest = 0;
2487
-
//Find farest possible and see if reach the tail
2488
-
for (int i = pStart; i <= pEnd; i++) {
2489
-
farthest = Math.max(farthest, i + A[i]);
2490
-
if (farthest >= A.length - 1) {
2491
-
return steps;
2492
-
}
2493
-
}
2494
-
//Re-select pointer position for start and end
2495
-
pStart = pEnd + 1;
2496
-
pEnd = farthest;
2497
-
}
2498
-
return -1; //This is the case where no solution can be found.
**133. [Jump Game.java](https://github.com/shawnfan/LintCode/blob/master/Java/Jump Game.java)**Given an array of non-negative integers, you are initially positioned at the first index of the array.
2509
-
2510
-
Each element in the array represents your maximum jump length at that position.
2470
+
**133. [Jump Game.java](https://github.com/shawnfan/LintCode/blob/master/Java/Jump Game.java)** Level: Medium
2511
2471
2512
-
Determine if you are able to reach the last index.
2472
+
给出步数,看能不能reach to end.
2513
2473
2514
-
Example
2515
-
A = [2,3,1,1,4], return true.
2516
-
2517
-
A = [3,2,1,0,4], return false.
2518
-
2519
-
This can be done using DP. However, greedy algorithm is fast in this particular problem. Consider both solutions.
2520
-
2521
-
DP
2522
-
Thinking Process:
2523
-
We have array A, that stores the # of steps for each index.
2524
-
State: f[i] means if previous steps can reach to i. True/False
2525
-
Function: f[i] = f[j] && (j + A[j] >= i)
2526
-
Init: f[0] = true
2527
-
Answer: f[n-1], if n is the length of A
2528
-
*/
2529
-
2530
-
public class Solution {
2531
-
/**
2532
-
* @param A: A list of integers
2533
-
* @return: The boolean answer
2534
-
**/
2535
-
//DP
2536
-
public boolean canJump(int[] A) {
2537
-
if (A == null || A.length == 0) {
2538
-
return false;
2539
-
}
2540
-
//By default, boolean[] can is all false
2541
-
boolean[] can = new boolean[A.length];
2542
-
can[0] = true;
2543
-
for (int i = 1; i < A.length; i++) {
2544
-
for (int j = 0; j < i; j++) {
2545
-
if (A[j] && (j + A[j] >= i)) {
2546
-
can[i] = true;
2547
-
break;
2548
-
}
2549
-
}
2550
-
}
2551
-
return can[A.length - 1];
2552
-
}
2553
-
}
2554
-
2555
-
2556
-
2557
-
/*
2558
-
2559
-
Greedy. Ideas from Yu’s Garden
2560
-
At each index, check how far we can jump, store this forest-can-jump position in variable ‘farest’. Take max of current farest and (index + A[index]), store is in farest
2561
-
At each index, compare if ‘farest’ is greater than the end of array, if so, found solution, return true.
2562
-
At each index, also check if ‘farest == current index’, that means the farest we can move is to current index and we cannot move forward. Then return false.
2563
-
*/
2564
-
2565
-
public class Solution {
2566
-
/**
2567
-
* @param A: A list of integers
2568
-
* @return: The boolean answer
2569
-
**/
2570
-
2571
-
public boolean canJump(int[] A) {
2572
-
if (A == null || A.length == 0) {
2573
-
return false;
2574
-
}
2575
-
int farest = 0;
2576
-
for (int i = 0; i < A.length; i++) {
2577
-
farest = Math.max(farest, i + A[i]);
2578
-
if (farest > A.length - 1) {
2579
-
return true;
2580
-
}
2581
-
if (farest == i) {
2582
-
return false;
2583
-
}
2584
-
}
2585
-
return true;
2586
-
}
2587
-
}
2474
+
Status:
2475
+
DP[i]: 在i点记录,i点之前的步数是否可以走到i点? True of false.
2476
+
其实j in [0~i)中间只需要一个能到达i 就好了
2477
+
Function:
2478
+
DP[i] = DP[j] && (j + A[j]), for all j in [0 ~ i)
2479
+
Return:
2480
+
DP[dp.length - 1];
2588
2481
2589
2482
2590
2483
---
@@ -4136,6 +4029,11 @@ node 到底,而head ~ node刚好是 n 距离。所以head就是要找的last n
4136
4029
---
4137
4030
**198. [Number of Islands II.java](https://github.com/shawnfan/LintCode/blob/master/Java/Number of Islands II.java)** Level: Hard
0 commit comments