Skip to content

Commit 3b3f424

Browse files
author
Мето Трајковски
committed
Fixed one and added one new solution
1 parent 05e74e3 commit 3b3f424

File tree

3 files changed

+104
-14
lines changed

3 files changed

+104
-14
lines changed

Diff for: Arrays/jump_game.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
Each element in the array represents your maximum jump length at that position.
66
Determine if you are able to reach the last index.
77
8-
Input: [2,3,1,1,4]
8+
Input: [2, 3, 1, 1, 4]
99
Output: True
1010
11-
Input: [3,2,1,0,4]
11+
Input: [3, 2, 1, 0, 4]
1212
Output: False
1313
1414
=========================================
@@ -28,17 +28,17 @@ def can_jump(nums):
2828
if n == 0:
2929
return False
3030

31-
last = 0
31+
max_jump = 0
3232
for i in range(n):
3333
# if this field isn't reachable return False
34-
if last < i:
34+
if max_jump < i:
3535
return False
3636

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)
3939

4040
# if the jump is greater or equal to the last element return True
41-
if last >= n - 1:
41+
if max_jump >= n - 1:
4242
return True
4343

4444

@@ -48,8 +48,8 @@ def can_jump(nums):
4848

4949
# Test 1
5050
# Correct result => True
51-
print(can_jump([2,3,1,1,4]))
51+
print(can_jump([2, 3, 1, 1, 4]))
5252

5353
# Test 2
5454
# Correct result => False
55-
print(can_jump([3,2,1,0,4]))
55+
print(can_jump([3, 2, 1, 0, 4]))

Diff for: Arrays/trapped_watter.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
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.
1515
1616
=========================================
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.
1718
First search for the max wall from front, after that correct the left water starting from the back side
1819
Time Complexity: O(N)
1920
Space Complexity: O(1)
@@ -26,25 +27,28 @@
2627

2728
def trapped_water(elevation_map):
2829
n = len(elevation_map)
30+
if n == 0:
31+
return 0
32+
2933
water = 0
3034

3135
# start from front of the array
3236
# and look for the max wall
33-
last_idx = 0
37+
max_idx = 0
3438
max_height = elevation_map[0]
3539

3640
for i in range(1, n):
3741
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
3943
max_height = elevation_map[i]
4044

4145
water += max_height - elevation_map[i]
4246

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
4448
# and correct the result (pour the extra water if there is smaller wall on the right side)
4549
back_max_height = elevation_map[-1]
4650

47-
for i in reversed(range(last_idx + 1, n - 1)):
51+
for i in range(n - 1, max_idx, -1):
4852
if elevation_map[i] > back_max_height:
4953
back_max_height = elevation_map[i]
5054

@@ -63,4 +67,8 @@ def trapped_water(elevation_map):
6367

6468
# Test 2
6569
# 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]))

Diff for: Dynamic Programming/jump_game_2.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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))

0 commit comments

Comments
 (0)