jump-game.dart (1297B)
1 //Given a list of numbers return if it is possible to make it 2 //to the end of the list where you are only able to jump at most the number of 3 //spots on the current nums value. To solve this I used DP, recursion, and memoization. 4 //I chose to use a list instead of a set because the set was getting too big and since we know 5 //how long the list will be a list works well for this because of its constant time checks. 6 7 //Note: I was running into many issues with sets 8 //as well as shortening the list each time. Sublists 9 //do not work on this problem because they take linear time 10 //to create as opposed to indexes which work much faster. 11 12 //Time: 623ms Beats: 14.29% 13 //Memory: 167.3MB Beats: 7.14% 14 class Solution { 15 List<bool> memo = []; 16 bool canJump(List<int> nums) { 17 memo = List.filled(nums.length , false); 18 return jump(nums, 0); 19 } 20 bool jump(List<int> nums , int index){ 21 if(memo[index]){ 22 return false; 23 } 24 memo[index] = true; 25 if(nums.length - 1 == index || nums[index] + index >= nums.length){ 26 return true; 27 } 28 if(nums[index] == 0){ 29 return false; 30 } 31 for(int i = 0 ; i < nums[index] ; ++i){ 32 if(jump(nums, index + i + 1)){ 33 return true; 34 } 35 } 36 return false; 37 } 38 }