-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1306.cpp
More file actions
29 lines (29 loc) · 854 Bytes
/
1306.cpp
File metadata and controls
29 lines (29 loc) · 854 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
class Solution {
public:
bool canReach(vector<int>& arr, int start) {
int n = arr.size();
vector<bool> visited(n, false);
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int m = q.size();
while (m--) {
int curr = q.front();
q.pop();
if (arr[curr] == 0) return true;
int left = curr - arr[curr];
int right = curr + arr[curr];
if (left >= 0 && !visited[left]) {
q.push(left);
visited[left] = true;
}
if (right < n && !visited[right]) {
q.push(right);
visited[right] = true;
}
}
}
return false;
}
};