GeezerVison is showing github.com — Fixed one and added one new solution · code3002/coding-problems@3b3f424
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Arrays/jump_game.py +9-9Lines changed: 9 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | Each element in the array represents your maximum jump length at that position. | 6 | 6 | Determine if you are able to reach the last index. | 7 | 7 |
| 8 | | -Input: [2,3,1,1,4] | | 8 | +Input: [2, 3, 1, 1, 4] | 9 | 9 | Output: True | 10 | 10 |
| 11 | | -Input: [3,2,1,0,4] | | 11 | +Input: [3, 2, 1, 0, 4] | 12 | 12 | Output: False | 13 | 13 |
| 14 | 14 | ========================================= | @@ -28,17 +28,17 @@ def can_jump(nums): | 28 | 28 | if n == 0: | 29 | 29 | return False | 30 | 30 |
| 31 | | - last = 0 | | 31 | + max_jump = 0 | 32 | 32 | for i in range(n): | 33 | 33 | # if this field isn't reachable return False | 34 | | - if last < i: | | 34 | + if max_jump < i: | 35 | 35 | return False | 36 | 36 |
| 37 | | - max_jump = i + nums[i] | 38 | | - last = max(last, max_jump) | | 37 | + this_jump = i + nums[i] | | 38 | + max_jump = max(max_jump, this_jump) | 39 | 39 |
| 40 | 40 | # if the jump is greater or equal to the last element return True | 41 | | - if last >= n - 1: | | 41 | + if max_jump >= n - 1: | 42 | 42 | return True | 43 | 43 |
| 44 | 44 |
| @@ -48,8 +48,8 @@ def can_jump(nums): | 48 | 48 |
| 49 | 49 | # Test 1 | 50 | 50 | # Correct result => True | 51 | | -print(can_jump([2,3,1,1,4])) | | 51 | +print(can_jump([2, 3, 1, 1, 4])) | 52 | 52 |
| 53 | 53 | # Test 2 | 54 | 54 | # Correct result => False | 55 | | -print(can_jump([3,2,1,0,4])) | | 55 | +print(can_jump([3, 2, 1, 0, 4])) |
Collapse file Copy file name to clipboardExpand all lines: Arrays/trapped_watter.py +13-5Lines changed: 13 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | Output explanation: We can hold 3 units in the first index, 2 in the second, and 3 in the fourth index (we cannot hold 5 since it would run off to the left), so we can trap 8 units of water. | 15 | 15 |
| 16 | 16 | ========================================= | | 17 | +The goal is to find the max wall and make 2 iterations starting from front and from back looking for the next bigger wall. | 17 | 18 | First search for the max wall from front, after that correct the left water starting from the back side | 18 | 19 | Time Complexity: O(N) | 19 | 20 | Space Complexity: O(1) | | 26 | 27 |
| 27 | 28 | def trapped_water(elevation_map): | 28 | 29 | n = len(elevation_map) | | 30 | + if n == 0: | | 31 | + return 0 | | 32 | + | 29 | 33 | water = 0 | 30 | 34 |
| 31 | 35 | # start from front of the array | 32 | 36 | # and look for the max wall | 33 | | - last_idx = 0 | | 37 | + max_idx = 0 | 34 | 38 | max_height = elevation_map[0] | 35 | 39 |
| 36 | 40 | for i in range(1, n): | 37 | 41 | if elevation_map[i] >= max_height: | 38 | | - last_idx = i # save the highest wall index for later | | 42 | + max_idx = i # save the highest wall index for later | 39 | 43 | max_height = elevation_map[i] | 40 | 44 |
| 41 | 45 | water += max_height - elevation_map[i] | 42 | 46 |
| 43 | | - # after that start from back and go reverse to the max wall | | 47 | + # after that start from back and go reverse to the max wall idx | 44 | 48 | # and correct the result (pour the extra water if there is smaller wall on the right side) | 45 | 49 | back_max_height = elevation_map[-1] | 46 | 50 |
| 47 | | - for i in reversed(range(last_idx + 1, n - 1)): | | 51 | + for i in range(n - 1, max_idx, -1): | 48 | 52 | if elevation_map[i] > back_max_height: | 49 | 53 | back_max_height = elevation_map[i] | 50 | 54 |
| @@ -63,4 +67,8 @@ def trapped_water(elevation_map): | 63 | 67 |
| 64 | 68 | # Test 2 | 65 | 69 | # Correct result => 8 | 66 | | -print(trapped_water([3, 0, 1, 3, 0, 5])) | | 70 | +print(trapped_water([3, 0, 1, 3, 0, 5])) | | 71 | + | | 72 | +# Test 3 | | 73 | +# Correct result => 6 | | 74 | +print(trapped_water([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])) |
Collapse file Copy file name to clipboard +82Lines changed: 82 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Jump Game 2 | | 3 | +
| | 4 | +Given an array of non-negative integers, you are initially positioned at the first index of the array. | | 5 | +Each element in the array represents your maximum jump length at that position. | | 6 | +Your goal is to reach the last index in the minimum number of jumps. | | 7 | +
| | 8 | +Input: XXX | | 9 | +Output: XXX | | 10 | +Output explanation: XXX | | 11 | +
| | 12 | +========================================= | | 13 | +Classical 1D Dynamic Programming solution. | | 14 | + Time Complexity: O(N) , maybe looks like O(N^2) but that's not possible | | 15 | + Space Complexity: O(N) | | 16 | +If you analyze the previous solution, you'll see that you don't need the whole DP array. | | 17 | + Time Complexity: O(N) | | 18 | + Space Complexity: O(1) | | 19 | +''' | | 20 | + | | 21 | + | | 22 | +############## | | 23 | +# Solution 1 # | | 24 | +############## | | 25 | + | | 26 | +def min_jumps_1(nums): | | 27 | + n = len(nums) | | 28 | + if n <= 1: | | 29 | + return 0 | | 30 | + | | 31 | + dp = [-1]*n | | 32 | + dp[0] = 0 | | 33 | + | | 34 | + for i in range(n): | | 35 | + this_jump = i + nums[i] | | 36 | + jumps = dp[i] + 1 | | 37 | + | | 38 | + if this_jump >= n - 1: | | 39 | + return jumps | | 40 | + | | 41 | + # starging from back, go reverse and | | 42 | + # change all -1 values and break when first positive is found | | 43 | + for j in range(this_jump, i, -1): | | 44 | + if dp[j] != -1: | | 45 | + break | | 46 | + dp[j] = jumps | | 47 | + | | 48 | + | | 49 | +############## | | 50 | +# Solution 2 # | | 51 | +############## | | 52 | + | | 53 | +def min_jumps_2(nums): | | 54 | + n = len(nums) | | 55 | + if n <= 1: | | 56 | + return 0 | | 57 | + | | 58 | + jumps = 0 | | 59 | + max_jump = 0 | | 60 | + new_max_jump = 0 | | 61 | + | | 62 | + for i in range(n): | | 63 | + if max_jump < i: | | 64 | + max_jump = new_max_jump | | 65 | + jumps += 1 | | 66 | + | | 67 | + this_jump = i + nums[i] | | 68 | + if this_jump >= n - 1: | | 69 | + return jumps + 1 | | 70 | + | | 71 | + new_max_jump = max(new_max_jump, this_jump) | | 72 | + | | 73 | + | | 74 | +########### | | 75 | +# Testing # | | 76 | +########### | | 77 | + | | 78 | +# Test 1 | | 79 | +# Correct result => 2 | | 80 | +nums = [2, 3, 1, 1, 4] | | 81 | +print(min_jumps_1(nums)) | | 82 | +print(min_jumps_2(nums)) |
|