sak1b

Untitled

Nov 13th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. // problem link: https://leetcode.com/problems/jump-game-iii/
  2.  
  3. class Solution {
  4. public:
  5. int dp[50004];
  6. int n = 0;
  7.  
  8. int fun(vector<int>& arr, int i)
  9. {
  10. if(i>=n || i<0) return 0;
  11. if(arr[i]==0) return dp[i]=1;
  12. if(dp[i]!=-1) return dp[i];
  13.  
  14. dp[i]=0; // test with commenting and not commenting this line
  15. return dp[i] = (fun(arr, i+arr[i]) | fun(arr,i-arr[i]));
  16.  
  17. }
  18.  
  19.  
  20. bool canReach(vector<int>& arr, int start) {
  21. memset(dp,-1,sizeof(dp));
  22. n = arr.size();
  23.  
  24. return fun(arr, start);
  25. }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment