45. 跳跃游戏 II

时间:2020-04-18 15:51:53   收藏:0   阅读:45
 1 //BFS + 贪心
 2 //维护一个区间[l,r],在这里面可以找到能够跳到最大位置,step++,同时更新l,r
 3 class Solution 
 4 {
 5 public:
 6     int jump(vector<int>& nums) 
 7     {
 8          if(nums.size() < 2) return 0;
 9          int l = 0,r = 0,step = 0;
10          while(l <= r)
11          {
12              int max_r = 0;
13              for(int i = l;i <= r;i ++) max_r = max(max_r,i + nums[i]);
14              l = r + 1,r = max_r;
15              step ++;
16              if(r >= (int)nums.size() - 1) break;
17          }
18          return step;
19     }
20 };

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!