-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
31 lines (31 loc) · 861 Bytes
/
solution.cpp
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
/**
* 56 / 56 test cases passed.
* Runtime: 48 ms
* Memory Usage: 30.6 MB
*/
class Solution {
public:
bool canReach(vector<int>& arr, int start) {
if (arr[start] == 0) return true;
int n = arr.size();
queue<int> que;
que.emplace(start);
vector<bool> used(n);
while (!que.empty()) {
auto curr = que.front(); que.pop();
int next = curr + arr[curr];
if (next < n && !used[next]) {
if (arr[next] == 0) return true;
used[next] = true;
que.emplace(next);
}
next = curr - arr[curr];
if (next >= 0 && !used[next]) {
if (arr[next] == 0) return true;
used[next] = true;
que.emplace(next);
}
}
return false;
}
};