-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvancing_through.h
More file actions
32 lines (26 loc) · 925 Bytes
/
Copy pathadvancing_through.h
File metadata and controls
32 lines (26 loc) · 925 Bytes
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
#ifndef CPP_ALGORITHM_ADVANCING_THROUGH_H
#define CPP_ALGORITHM_ADVANCING_THROUGH_H
#include <vector>
namespace AdvancingThrough
{
/**
* \brief Advance through the array to the last index.
* \param max_advance_steps maximum number of steps that can be taken from each index
* \return either reach the end or not
*/
bool CanReachEnd(
const std::vector<int>& max_advance_steps);
}
// ----------------------------------------------------------------------------
inline bool AdvancingThrough::CanReachEnd(
const std::vector<int>& max_advance_steps)
{
int reach_so_far = 0; // furthest reach so far
const int last_index = static_cast<int>(max_advance_steps.size()) - 1;
for (int i = 0; i <= reach_so_far && reach_so_far < last_index; ++i)
{
reach_so_far = std::max(reach_so_far, max_advance_steps[i] + i);
}
return reach_so_far >= last_index;
}
#endif