leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1306.cpp (676B)
0 class Solution {
1 public:
2 bool canReach(const vector<int> &arr, int start) {
3 bitset<50001> bs;
4 queue<int> q({start});
5 while (!q.empty()) {
6 const int idx = q.front();
7 q.pop();
8 const int left = idx - arr[idx], right = idx + arr[idx];
10 if (right < arr.size() && !bs[right]) {
11 if (!arr[right]) return true;
12 bs.set(right);
13 q.push(right);
14 }
16 if (left >= 0 && !bs[left]) {
17 if (!arr[left]) return true;
18 bs.set(left);
19 q.push(left);
20 }
21 }
23 return false;
24 }
25 };