leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1306.cpp (676B)


      1 class Solution {
      2   public:
      3     bool canReach(const vector<int> &arr, int start) {
      4         bitset<50001> bs;
      5         queue<int> q({start});
      6         while (!q.empty()) {
      7             const int idx = q.front();
      8             q.pop();
      9             const int left = idx - arr[idx], right = idx + arr[idx];
     10 
     11             if (right < arr.size() && !bs[right]) {
     12                 if (!arr[right]) return true;
     13                 bs.set(right);
     14                 q.push(right);
     15             }
     16 
     17             if (left >= 0 && !bs[left]) {
     18                 if (!arr[left]) return true;
     19                 bs.set(left);
     20                 q.push(left);
     21             }
     22         }
     23 
     24         return false;
     25     }
     26 };