GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — coding-problems/Dynamic Programming/jump_game_2.py at master · Karing19/coding-problems Expand file tree/ Copy pathjump_game_2.pyMore file actions 82 lines (62 loc) · 1.72 KB / Copy pathjump_game_2.pyFile metadata and controls82 lines (62 loc) · 1.72 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 ''' Jump Game 2 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. Input: XXX Output: XXX Output explanation: XXX ========================================= Classical 1D Dynamic Programming solution. Time Complexity: O(N) , maybe looks like O(N^2) but that's not possible Space Complexity: O(N) If you analyze the previous solution, you'll see that you don't need the whole DP array. Time Complexity: O(N) Space Complexity: O(1) ''' ############## # Solution 1 # ############## def min_jumps_1(nums): n = len(nums) if n <= 1: return 0 dp = [-1]*n dp[0] = 0 for i in range(n): this_jump = i + nums[i] jumps = dp[i] + 1 if this_jump >= n - 1: return jumps # starging from back, go reverse and # change all -1 values and break when first positive is found for j in range(this_jump, i, -1): if dp[j] != -1: break dp[j] = jumps ############## # Solution 2 # ############## def min_jumps_2(nums): n = len(nums) if n <= 1: return 0 jumps = 0 max_jump = 0 new_max_jump = 0 for i in range(n): if max_jump < i: max_jump = new_max_jump jumps += 1 this_jump = i + nums[i] if this_jump >= n - 1: return jumps + 1 new_max_jump = max(new_max_jump, this_jump) ########### # Testing # ########### # Test 1 # Correct result => 2 nums = [2, 3, 1, 1, 4] print(min_jumps_1(nums)) print(min_jumps_2(nums)) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |