-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#106.cpp
More file actions
33 lines (26 loc) · 870 Bytes
/
Copy path#106.cpp
File metadata and controls
33 lines (26 loc) · 870 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
33
#include<iostream>
#include<unordered_map>
#include<stdlib.h>
using namespace std;
bool set_and_return(unordered_map<int, bool>& memo, int key, bool ret) {
memo[key] = ret;
return memo[key];
}
bool result_from_index(int curr, int hops[], int n, unordered_map<int, bool>& memo) {
if (memo.find(curr) != memo.end()) return memo[curr];
if (curr == n - 1) return true;
int max_hops = min(hops[curr], (n-1) - curr);
for (int i = 1; i <= max_hops; i++) {
if (result_from_index(curr + i, hops, n, memo))
return set_and_return(memo, curr, true);
}
return set_and_return(memo, curr, false);
}
bool can_reach_last_index(int hops[], int n) {
unordered_map<int, bool> memo;
return result_from_index(0, hops, n, memo);
}
int main() {
int hops[] = {2, 0, 1, 0};
cout << (can_reach_last_index(hops, sizeof(hops)/sizeof(hops[0])) ? "True" : "False") << endl;
}