Skip to content

Commit

Permalink
Refactored solutions 229 - 230
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 28, 2021
1 parent 8363ff6 commit fbcc592
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 33 deletions.
77 changes: 52 additions & 25 deletions Solutions/229.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,76 @@
ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
"""

from typing import Dict

def get_next_ladder(ladders, pos):

def get_next_ladder_position(ladders: Dict[int, int], position: int):
# helper function to get the position of the next ladder
curr = 101
for key in ladders:
if key > pos and key < curr:
if key > position and key < curr:
curr = key
return curr


def get_next_no_snake(snakes, pos):
# helper function to get the position of the next position without snake
curr = pos + 6
def get_next_position_with_no_snake(snakes: Dict[int, int], position: int) -> int:
# helper function to get the position of the next move without landing on a snake
curr = position + 6
for _ in range(6):
if curr in snakes:
curr -= 1
else:
if curr not in snakes:
break
curr -= 1
return curr


def play_snake_and_ladders(snakes, ladders):
def play_snake_and_ladders(
snakes: Dict[int, int], ladders: Dict[int, int], show_trace: bool = False
) -> int:
# function to return the minimum turns required to play the current board
# prints the trace of the path ('->' for normal move, '=>' for ladder move)
pos = 0
position = 0
turns = 0

while pos < 100:
while position < 100:
turns += 1
pos = min(get_next_ladder(ladders, pos), get_next_no_snake(snakes, pos), 100)
print(pos, end=" ")

if pos in ladders:
pos = ladders[pos]
print(f"=> {pos}", end=" ")
if pos < 100:
position = min(
get_next_ladder_position(ladders, position),
get_next_position_with_no_snake(snakes, position),
100,
)
if show_trace:
print(position, end=" ")
if position in ladders:
position = ladders[position]
if show_trace:
print(f"=> {position}", end=" ")
if position < 100 and show_trace:
print("->", end=" ")

print()
if show_trace:
print()
return turns


snakes = {16: 6, 48: 26, 49: 11, 56: 53, 62: 19, 64: 60, 87: 24, 93: 73, 95: 75, 98: 78}
ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
if __name__ == "__main__":
snakes = {
16: 6,
48: 26,
49: 11,
56: 53,
62: 19,
64: 60,
87: 24,
93: 73,
95: 75,
98: 78,
}
ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}

print(play_snake_and_ladders(snakes, ladders))
print(play_snake_and_ladders(snakes, ladders, show_trace=True))


"""
SPECS:
TIME COMPLEXITY: O(1)
SPACE COMPLEXITY: O(1)
[the maximum number of squares is 100]
"""
26 changes: 18 additions & 8 deletions Solutions/230.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,31 @@
from sys import maxsize


def calculate(eggs, floors):
dp_mat = [[0 for _ in range(floors + 1)] for _ in range(eggs + 1)]
def calculate(eggs: int, floors: int) -> int:
dp_mat = [[maxsize for _ in range(floors + 1)] for _ in range(eggs + 1)]
# base cases
for i in range(floors + 1):
dp_mat[1][i] = i

dp_mat[0][i] = 0
for i in range(eggs + 1):
dp_mat[i][0] = 0
# populating the dp matrix
for egg in range(2, eggs + 1):
for floor in range(1, floors + 1):
dp_mat[egg][floor] = maxsize
for i in range(1, floor + 1):
temp = 1 + max(dp_mat[egg - 1][i - 1], dp_mat[egg][floor - i])
dp_mat[egg][floor] = min(dp_mat[egg][floor], temp)

return dp_mat[eggs][floors]


# DRIVER CODE
print(calculate(2, 20))
print(calculate(3, 15))
if __name__ == "__main__":
print(calculate(2, 20))
print(calculate(3, 15))


"""
SPECS:
TIME COMPLEXITY: O(n x (floor ^ 2))
SPACE COMPLEXITY: O(n x floor)
"""

0 comments on commit fbcc592

Please sign in to comment.