Leetcodes Solution 45 Jump Game II

Leetcodes Solution 45 Jump Game II

來自專欄 Leetcodes Solutions

匯總

雪之下雪乃:leetcode解題總匯?

zhuanlan.zhihu.com圖標

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

For example:

Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

Note:

You can assume that you can always reach the last index.

思路1

BFS

class Solution{public: int jump(vector<int>& nums){ int n = nums.size(), step = 0, start = 0, end = 0; while(end < n - 1){ step++; int maxend = end + 1; for(int i = start; i <= end; i++){ if(i + nums[i] >= n - 1) return step; maxend = max(maxend, i + nums[i]); } start = end + 1; end = maxend; } return step; }};

推薦閱讀:

插入排序insertion sort
AI小編問世!阿里智能寫手核心技術首次公開!
演算法集錦(7)| 實用代碼 | Google Colab使用及配置技巧

TAG:演算法 | 數據結構 | 編程 |