-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame2.cpp
More file actions
110 lines (77 loc) · 2.63 KB
/
Copy pathJumpGame2.cpp
File metadata and controls
110 lines (77 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Given an array of non-negative integers nums, 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.
You can assume that you can always reach the last index.
Input: nums = [2,3,1,1,4]
Output: 2
Explanation: 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.
*/
// First Solution: This is an O(n**2) solution using dp
class Solution {
public:
int jump(vector<int>& nums)
{
int n = nums.size();
int* dp = new int[n];
int min;
if(nums[0]==0 || n==0) return 0;
dp[n-1] = 0;
for(int i=(n-2);i>=0;i--)
{
if(nums[i]==0)
{
dp[i] = INT_MAX;
}
else if( nums[i]>= n-i-1 ) dp[i] = 1;
else
{
min = INT_MAX ;
for(int j = i + 1; j < n && j <= nums[i] + i; j++)
{
if(min>dp[j]) min=dp[j];
}
if(min!=INT_MAX) dp[i]=min+1;
else dp[i]=min;
}
}
if(dp[0]==INT_MAX) return 0;
else return dp[0];
}
};
/* Fast Solution O(n) time and O(1) space
The idea is to store info in 3 variables jump, steps and maxReach.
The steps var is used to iterate through the array and eventually reach the end of the array.
The maxReach var keeps the value of the maxPossible distance we can cover from our current ith position
Finally, the jump is our answer.
Once the steps are exhausted i.e steps==0, it implies that we must have/should take a jump therefore we increase jump var
To keep continiung in the array we update the steps var with maxSteps - i.
If we are not able to reach the curr indice. i.e we have exhausted our steps and our maxReach<=i then it means
that we cannot move forward.
*/
class Solution {
public:
int jump(vector<int>& nums)
{
int n = nums.size();
if(n==0 || nums[0]==0) return 0;
else if(n<=1) return 0;
int jumps = 1;
int steps = nums[0];
int maxReach = nums[0];
for(int i=1;i<n;i++)
{
if(i == n-1) return jumps;
steps--;
maxReach = max(maxReach, nums[i]+i);
if(steps==0)
{
jumps++;
if(maxReach<=i) return -1;
steps = maxReach - i;
}
}
return 0;
}
};