jump-gameV2.dart (779B)
1 //This reverses the direction of searches in the for 2 //loop to check the furthest forward jump first to speed up 3 //movement. 4 //Time: 541ms Beats: 14.29% 5 //Memory: 166.8MB Beats: 7.14% 6 class Solution { 7 List<bool> memo = []; 8 bool canJump(List<int> nums) { 9 memo = List.filled(nums.length , false); 10 return jump(nums, 0); 11 } 12 bool jump(List<int> nums , int index){ 13 if(memo[index]){ 14 return false; 15 } 16 memo[index] = true; 17 if(nums.length - 1 == index || nums[index] + index >= nums.length){ 18 return true; 19 } 20 if(nums[index] == 0){ 21 return false; 22 } 23 for(int i = nums[index]; i > 0 ; --i){ 24 if(jump(nums, index + i)){ 25 return true; 26 } 27 } 28 return false; 29 } 30 }