Skip to content

Commit

Permalink
Refactored solutions 245 - 246
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 30, 2021
1 parent 60a1f41 commit 2e99058
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 38 deletions.
29 changes: 18 additions & 11 deletions Solutions/245.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
"""
Problem:
You are given an array of integers, where each element represents the maximum number of steps that can be jumped going forward from that element.
Write a function to return the minimum number of jumps you must take in order to get from the start to the end of the array.
You are given an array of integers, where each element represents the maximum number of
steps that can be jumped going forward from that element. Write a function to return
the minimum number of jumps you must take in order to get from the start to the end of
the array.
Example:
Input = [6, 2, 4, 0, 5, 1, 1, 4, 2, 9]
Output = 2 (as the optimal solution involves jumping from 6 to 5, and then from 5 to 9)
For example, given [6, 2, 4, 0, 5, 1, 1, 4, 2, 9], you should return 2, as the optimal
solution involves jumping from 6 to 5, and then from 5 to 9.
"""

from sys import maxsize
from typing import List


def get_min_jumps(arr):
def get_min_jumps(arr: List[int]) -> int:
length = len(arr)
# using dynamic programming to reduce O(n ^ n) problem to O(n ^ 2)
dp = [0 for _ in range(length)]
for i in range(length - 2, -1, -1):
# updating the minimum hops for each position
if arr[i]:
dp[i] = min(dp[i + 1 : i + arr[i] + 1]) + 1
else:
dp[i] = maxsize
return dp[0]


# DRIVER CODE
print(get_min_jumps([6, 2, 4, 0, 5, 1, 1, 4, 2, 9]))
if __name__ == "__main__":
print(get_min_jumps([6, 2, 4, 0, 5, 1, 1, 4, 2, 9]))


"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(n)
"""
67 changes: 40 additions & 27 deletions Solutions/246.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
"""
Problem:
Given a list of words, determine whether the words can be chained to form a circle.
A word X can be placed in front of another word Y in a circle if the last character of X is same as the first character of Y.
Given a list of words, determine whether the words can be chained to form a circle. A
word X can be placed in front of another word Y in a circle if the last character of X
is same as the first character of Y.
Example:
Input = ['chair', 'height', 'racket', 'touch', 'tunic']
Output = True (chair -> racket -> touch -> height -> tunic -> chair)
For example, the words ['chair', 'height', 'racket', 'touch', 'tunic'] can form the
following circle: chair -> racket -> touch -> height -> tunic -> chair.
"""


def check_circle_formation_helper(word_list, start, end, curr, start_word, seen):
# checking if all words have been used
from typing import Dict, List, Set


def check_circle_formation_helper(
word_list: List[str],
start: Dict[str, Set[str]],
end: Dict[str, Set[str]],
curr: str,
start_word: str,
seen: Set[str],
) -> bool:
if len(seen) == len(word_list):
# checking if a cycle is formed
if start_word[0] == curr[-1]:
return True
return False
try:
# iterating over all possible words from the current word
for word in start[curr[-1]]:
if word not in seen:
seen_copy = seen.copy()
seen_copy.add(word)
# checking if a cycle can be formed
if check_circle_formation_helper(
word_list, start, end, word, start_word, seen_copy
):
return True
except KeyError: # incase the current word's last character isn't present in start
except KeyError:
# the current word's last character isn't present in start dictionary
pass
return False


def check_circle_formation(word_list):
# generating the start and end maps
def check_circle_formation(word_list: List[str]) -> bool:
start = {}
end = {}
for word in word_list:
Expand All @@ -54,16 +59,24 @@ def check_circle_formation(word_list):
return False


# DRIVER CODE
print(
check_circle_formation(["chair", "height", "racket", "touch", "tunic"])
) # chair, racket, touch, height, tunic, chair
print(
check_circle_formation(["height", "racket", "touch", "tunic", "car"])
) # racket, touch, height, tunic, car, racket
print(
check_circle_formation(["height", "racket", "touch", "tunic"])
) # racket, touch, height, tunic (but no looping)
print(
check_circle_formation(["height", "racket", "touch", "tunic", "cat"])
) # no looping
if __name__ == "__main__":
print(
check_circle_formation(["chair", "height", "racket", "touch", "tunic"])
) # chair, racket, touch, height, tunic, chair
print(
check_circle_formation(["height", "racket", "touch", "tunic", "car"])
) # racket, touch, height, tunic, car, racket
print(
check_circle_formation(["height", "racket", "touch", "tunic"])
) # racket, touch, height, tunic (no looping even though there is a chain)
print(
check_circle_formation(["height", "racket", "touch", "tunic", "cat"])
) # no looping


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

0 comments on commit 2e99058

Please sign in to comment.