第一次给每个值都算最远距离,结果large没过,第二次用了现在的方法稍微改了改,最后过了,少数次过吧
1 class Solution { 2 public: 3 int jump(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int left, right, index; 7 left = right = index = 0; 8 while (right < n-1) { 9 index++; 10 int rmax = left+A[left]; 11 for (int i = left+1; i <= right; i++) { 12 rmax = max(rmax, i+A[i]); 13 } 14 left = right; 15 right = rmax; 16 } 17 return index; 18 } 19 };
C#
1 public class Solution { 2 public int Jump(int[] nums) { 3 int left = 0, right = 0, index = 0; 4 while (right < nums.Length-1) { 5 index++; 6 int rmax = left + nums[left]; 7 for (int i = left + 1; i <= right; i++) rmax = Math.Max(rmax, i + nums[i]); 8 left = right; 9 right = rmax; 10 } 11 return index; 12 } 13 }