Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool dfs(vector<int>& arr, int start,unordered_map<int,int> m){
- if(m[start]>0){
- return false;
- }
- m[start]++;
- if(arr[start]==0){
- return true;
- }
- bool b1;
- if(start-arr[start]>=0){
- b1=dfs(arr,start-arr[start],m);
- }else{
- b1=false;
- }
- if(b1==true){
- return true;
- }
- bool b2;
- if(start+arr[start]<arr.size()){
- b2=dfs(arr,start+arr[start],m);
- if(b2==true){
- return true;
- }
- }else{
- b2=false;
- }
- return b1||b2;
- }
- bool canReach(vector<int>& arr, int start) {
- unordered_map<int,int> m;
- return dfs(arr,start,m);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment