Guest User

Untitled

a guest
Jun 23rd, 2019
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. struct Data{
  2. int index;
  3. int dis;
  4. int step;
  5. };
  6.  
  7.  
  8. class Solution {
  9. public:
  10. int jump(vector<int>& arr) {
  11.  
  12. int n=arr.size();
  13. if(n==0){
  14. return 0;
  15. }
  16. int vis[n]={0};
  17. queue<Data> q;
  18. Data d;
  19. d.index=0;
  20. d.dis=arr[0];
  21. d.step=0;
  22. q.push(d);
  23. vis[0]=1;
  24. Data f;
  25. while(!q.empty()){
  26. f=q.front();
  27. q.pop();
  28.  
  29. if(f.index==n-1){
  30. //cout<<f.step<<endl;
  31. break;
  32.  
  33. }
  34. int index=f.index+1;
  35. int dis=f.index+f.dis;
  36. for(;index<=dis&&index<n;index++){
  37. if(vis[index]==0){
  38. vis[index]=1;
  39. Data newd;
  40. newd.dis=arr[index];
  41. newd.index=index;
  42. newd.step=1+f.step;
  43. q.push(newd);
  44. }
  45. }
  46.  
  47. }
  48. return f.step;
  49. }
  50. };
Add Comment
Please, Sign In to add comment