-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay25.cpp
More file actions
41 lines (34 loc) · 1.03 KB
/
Day25.cpp
File metadata and controls
41 lines (34 loc) · 1.03 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
/*
This problem was asked by Yelp.
You are given an array of integers, where each element represents the maximum number of steps that can be jumped going forward from that element. Write a function to return the minimum number of jumps you must take in order to get from the start to the end of the array.
For example, given [6, 2, 4, 0, 5, 1, 1, 4, 2, 9], you should return 2, as the optimal solution involves jumping from 6 to 5, and then from 5 to 9.
*/
#include <bits/stdc++.h>
using namespace std;
int minJumps(vector<int> &arr)
{
int n = arr.size();
if (n <= 1)
return 0;
int jumps = 0;
int currEnd = 0;
int farthest = 0;
for (int i = 0; i < n - 1; i++)
{
farthest = max(farthest, i + arr[i]);
if (i == currEnd)
{
jumps++;
currEnd = farthest;
if (currEnd >= n - 1)
break;
}
}
return jumps;
}
int main()
{
vector<int> arr = {6, 2, 4, 0, 5, 1, 1, 4, 2, 9};
cout << minJumps(arr) << endl;
return 0;
}