jibha

Untitled

Jan 31st, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4. bool dfs(vector<int>& arr, int start,unordered_map<int,int> m){
  5.  
  6. if(m[start]>0){
  7. return false;
  8. }
  9.  
  10. m[start]++;
  11. if(arr[start]==0){
  12. return true;
  13. }
  14. bool b1;
  15.  
  16. if(start-arr[start]>=0){
  17. b1=dfs(arr,start-arr[start],m);
  18.  
  19. }else{
  20. b1=false;
  21. }
  22.  
  23. if(b1==true){
  24. return true;
  25. }
  26.  
  27. bool b2;
  28. if(start+arr[start]<arr.size()){
  29. b2=dfs(arr,start+arr[start],m);
  30. if(b2==true){
  31. return true;
  32. }
  33. }else{
  34. b2=false;
  35. }
  36.  
  37. return b1||b2;
  38.  
  39.  
  40. }
  41.  
  42. bool canReach(vector<int>& arr, int start) {
  43.  
  44. unordered_map<int,int> m;
  45.  
  46.  
  47.  
  48. return dfs(arr,start,m);
  49.  
  50.  
  51. }
  52. };
Advertisement
Add Comment
Please, Sign In to add comment