Skip to content

Commit

Permalink
Refactored solutions 290 - 304
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Feb 1, 2021
1 parent 942cbe4 commit 86df7a5
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 30 deletions.
4 changes: 3 additions & 1 deletion Solutions/290.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from DataStructures.Stack import Stack


QUXES = set(["R", "G", "B"])


Expand Down Expand Up @@ -51,7 +52,8 @@ def get_transformation(arrangement: List[str]) -> List[str]:
return stack


print(get_transformation(["R", "G", "B", "G", "B"]))
if __name__ == "__main__":
print(get_transformation(["R", "G", "B", "G", "B"]))


"""
Expand Down
3 changes: 1 addition & 2 deletions Solutions/291.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

def calculate_boats(arr: List[int], k: int) -> int:
length = len(arr)
# sorting the array to optimize the computation
arr.sort()
# calculating the number of boats

ptr1 = 0
ptr2 = length - 1
result = 0
Expand Down
8 changes: 4 additions & 4 deletions Solutions/293.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import List


def min_pyramid_cost(arr: List[int]) -> int:
def get_min_pyramid_cost(arr: List[int]) -> int:
length = len(arr)
left = [0 for _ in range(length)]
right = [0 for _ in range(length)]
Expand Down Expand Up @@ -56,9 +56,9 @@ def min_pyramid_cost(arr: List[int]) -> int:


if __name__ == "__main__":
print(min_pyramid_cost([1, 1, 3, 3, 2, 1]))
print(min_pyramid_cost([1, 1, 1, 1, 1]))
print(min_pyramid_cost([1, 1, 1, 5, 1]))
print(get_min_pyramid_cost([1, 1, 3, 3, 2, 1]))
print(get_min_pyramid_cost([1, 1, 1, 1, 1]))
print(get_min_pyramid_cost([1, 1, 1, 5, 1]))


"""
Expand Down
9 changes: 4 additions & 5 deletions Solutions/294.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@


def floyd_warshall(graph: List[List[int]]) -> List[List[int]]:
# floyd warshall algorithm for all pair shortest path
dist = [[elem for elem in row] for row in graph]
nodes = len(graph)
for i in range(nodes):
Expand All @@ -46,8 +45,9 @@ def floyd_warshall(graph: List[List[int]]) -> List[List[int]]:
return dist


def generate_graph(paths: Dict[Tuple[int, int], int], nodes: int) -> List[List[int]]:
# generating adjacency matix for the paths
def generate_graph_adjacency_matix(
paths: Dict[Tuple[int, int], int], nodes: int
) -> List[List[int]]:
graph = [[maxsize for _ in range(nodes)] for _ in range(nodes)]
for src, dest in paths:
graph[src][dest] = paths[src, dest]
Expand Down Expand Up @@ -80,8 +80,7 @@ def get_route_dfs_helper(


def get_route(elevations: Dict[int, int], paths: Dict[Tuple[int, int], int]) -> int:
# uses adjacency matrix as its easier to use in floyd warshall algorithm
graph = generate_graph(paths, len(elevations))
graph = generate_graph_adjacency_matix(paths, len(elevations))
dist = floyd_warshall(graph)
return get_route_dfs_helper(0, 0, 0, set(), dist)

Expand Down
4 changes: 3 additions & 1 deletion Solutions/296.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def create_balanced_bst(arr: List[int]) -> BinarySearchTree:
"""
SPECS:
TIME COMPLEXITY: O(n)
TIME COMPLEXITY: O(n x log(n))
SPACE COMPLEXITY: O(n)
[time complexity can be reduced to O(n) using node reference inplace of calling
tree.add()]
"""
2 changes: 0 additions & 2 deletions Solutions/297.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
def check_all_customers_satisfied(
customers: Set[int], combination: List[int], drinks: Dict[int, Set[int]]
) -> bool:
# check if the current combination can satisfy all customers
temp = None
for drink in combination:
if temp is None:
Expand Down Expand Up @@ -77,7 +76,6 @@ def get_min_drinks(preferences: Dict[int, List[int]]) -> int:
set(preferences.keys()),
combinations,
)
# returning the combination with minimum drinks
# NOTE: "min()" can be wrapped with "len()" to get the number of drinks
return min(combinations, key=lambda x: len(x))

Expand Down
4 changes: 2 additions & 2 deletions Solutions/299.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from DataStructures.PriorityQueue import MinPriorityQueue


def dijkstras_algo(
def dijkstra(
graph: GraphUndirectedWeighted, start: str
) -> Tuple[Dict[str, int], Dict[str, Optional[str]]]:
# dijkstra's algorithm for single source shortest path
Expand Down Expand Up @@ -67,7 +67,7 @@ def get_minimum_cost(pipes: Dict[str, Dict[str, int]]) -> int:
for dest in pipes[src]:
graph.add_edge(src, dest, pipes[src][dest])
# minimum cost calculation
dist, parent = dijkstras_algo(graph, "plant")
dist, parent = dijkstra(graph, "plant")
return tree_weight_sum(dist, parent)


Expand Down
20 changes: 10 additions & 10 deletions Solutions/301.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
element.
"""

# copied from:
# this is an improvised version of the method available at:
# https://www.geeksforgeeks.org/bloom-filters-introduction-and-python-implementation/

from bitarray import bitarray
Expand All @@ -25,7 +25,7 @@ class BloomFilter:
Class for Bloom filter, using murmur3 hash function
"""

def __init__(self, items_count, fp_prob):
def __init__(self, items_count: int, fp_prob: float) -> None:
"""
items_count : int
Number of items expected to be stored in bloom filter
Expand All @@ -36,18 +36,18 @@ def __init__(self, items_count, fp_prob):
self.fp_prob = fp_prob

# Size of bit array to use
self.size = self.get_size(items_count, fp_prob)
self.size = BloomFilter.get_size(items_count, fp_prob)

# number of hash functions to use
self.hash_count = self.get_hash_count(self.size, items_count)
self.hash_count = BloomFilter.get_hash_count(self.size, items_count)

# Bit array of given size
self.bit_array = bitarray(self.size)

# initialize all bits as 0
self.bit_array.setall(0)

def add(self, item):
def add(self, item: str) -> None:
"""
Add an item in the filter
"""
Expand All @@ -63,7 +63,7 @@ def add(self, item):
# set the bit True in bit_array
self.bit_array[digest] = True

def check(self, item):
def check(self, item: str) -> bool:
"""
Check for existence of an item in filter
"""
Expand All @@ -77,8 +77,8 @@ def check(self, item):
return False
return True

@classmethod
def get_size(self, n, p):
@staticmethod
def get_size(n: int, p: float) -> int:
"""
Return the size of bit array(m) to used using
following formula
Expand All @@ -91,8 +91,8 @@ def get_size(self, n, p):
m = -(n * log(p)) / (log(2) ** 2)
return int(m)

@classmethod
def get_hash_count(self, m, n):
@staticmethod
def get_hash_count(m: int, n: int) -> int:
"""
Return the hash function(k) to be used using
following formula
Expand Down
3 changes: 0 additions & 3 deletions Solutions/304.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


def get_moves(position: Tuple[int, int]) -> List[Tuple[int, int]]:
# generating all possible moves for the knight from the given position
i, j = position
moves = [
(i + 2, j + 1),
Expand All @@ -30,10 +29,8 @@ def get_moves(position: Tuple[int, int]) -> List[Tuple[int, int]]:
def get_knight_on_board_probability_helper(position: Tuple[int, int], k: int) -> int:
i, j = position
if not (0 <= i < 8) or not (0 <= j < 8):
# move restricted after moving off the board
return 0
if k == 0:
# a valid end position reached
return 1
# generating total number of valid moves from current position
moves = get_moves(position)
Expand Down

0 comments on commit 86df7a5

Please sign in to comment.