13 ms Share my C++ solution (beat 98.91%)


  • 0
    W
    class Solution {
    public:
    	int jump(vector<int>& nums) {
    		int n = nums.size();
    		if (n <= 1) return 0;
    		int reach = nums[0], i = 1, count = 0;
    
    		while (i < n && reach < n - 1){
    			count++;
    			int max = reach;
    			for (int j = i; j <= reach; j++) {
    				if (nums[j] + j > max){
    					max = nums[j] + j;
    					if(max >= n - 1) return count + 1;
    				}
    			}
    			i = reach + 1;
    			reach = max;
    		}
    		return count + 1;
    	}
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.