Skip to content

Commit

Permalink
Refactored solutions 257 - 258
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 31, 2021
1 parent d724510 commit 9c355b0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
34 changes: 22 additions & 12 deletions Solutions/257.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
"""
Problem:
Given an array of integers out of order, determine the bounds of the smallest window that must be sorted in order for the entire array to be sorted. For example, given [3, 7, 5, 6, 9], you should return (1, 3).
Given an array of integers out of order, determine the bounds of the smallest window
that must be sorted in order for the entire array to be sorted. For example, given
[3, 7, 5, 6, 9], you should return (1, 3).
"""


def get_sort_range(arr):
# sorting the array and checking if the input array requires sorting
from typing import List, Tuple


def get_sort_range(arr: List[int]) -> Tuple[int, int]:
arr_sorted = sorted(arr)
if arr_sorted == arr:
return (-1, -1)

return -1, -1
# getting the start and end of the unsorted part of the array
start = 0
end = 0
start, end = 0, 0
for i in range(len(arr)):
if arr[i] != arr_sorted[i]:
start = i
break
for i in range(start, len(arr)):
if arr[i] != arr_sorted[i]:
end = i
return (start, end)
return start, end


if __name__ == "__main__":
print(get_sort_range([3, 5, 6, 7, 9]))
print(get_sort_range([3, 7, 5, 6, 9]))
print(get_sort_range([5, 4, 3, 2, 1]))

# DRIVER CODE
print(get_sort_range([3, 5, 6, 7, 9]))
print(get_sort_range([3, 7, 5, 6, 9]))
print(get_sort_range([5, 4, 3, 2, 1]))

"""
SPECS:
TIME COMPLEXITY: O(n x log(n))
SPACE COMPLEXITY: O(n)
"""
54 changes: 31 additions & 23 deletions Solutions/258.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
Problem:
In Ancient Greece, it was common to write text with the first line going left to right, the second line going right to left, and continuing to go back and forth. This style was called "boustrophedon".
In Ancient Greece, it was common to write text with the first line going left to right,
the second line going right to left, and continuing to go back and forth. This style
was called "boustrophedon".
Given a binary tree, write an algorithm to print the nodes in boustrophedon order.
Expand All @@ -16,26 +18,29 @@
You should return [1, 3, 2, 4, 5, 6, 7].
"""

from DataStructures.Tree import Node, Binary_Tree
from typing import Dict, List
from .DataStructures.Tree import Node, BinaryTree


def get_boustrophedon_helper(self, level, accumulator):
def get_boustrophedon_helper(
node: Node, level: int, accumulator: Dict[int, List[int]]
) -> None:
# using dfs to store a list of values by level
if level not in accumulator:
accumulator[level] = []
accumulator[level].append(self.val)
if self.left:
self.left.get_boustrophedon_helper(level + 1, accumulator)
if self.right:
self.right.get_boustrophedon_helper(level + 1, accumulator)
accumulator[level].append(node.val)
if node.left:
get_boustrophedon_helper(node.left, level + 1, accumulator)
if node.right:
get_boustrophedon_helper(node.right, level + 1, accumulator)


def get_boustrophedon(self):
if not self.root:
def get_boustrophedon(tree: BinaryTree) -> List[int]:
if not tree.root:
return []
# generating the nodes by level
level_data = {}
self.root.get_boustrophedon_helper(1, level_data)
get_boustrophedon_helper(tree.root, 1, level_data)
result = []
# adding the even levels in reverse order in the result
for level in sorted(list(level_data.keys())):
Expand All @@ -46,22 +51,25 @@ def get_boustrophedon(self):
return result


setattr(Node, "get_boustrophedon_helper", get_boustrophedon_helper)
setattr(Binary_Tree, "get_boustrophedon", get_boustrophedon)
if __name__ == "__main__":
tree = BinaryTree()
tree.root = Node(1)

tree.root.left = Node(2)
tree.root.right = Node(3)

# DRIVER CODE
tree = Binary_Tree()
tree.root.left.left = Node(4)
tree.root.left.right = Node(5)

tree.root = Node(1)
tree.root.right.left = Node(6)
tree.root.right.right = Node(7)

tree.root.left = Node(2)
tree.root.right = Node(3)
print(get_boustrophedon(tree))

tree.root.left.left = Node(4)
tree.root.left.right = Node(5)

tree.root.right.left = Node(6)
tree.root.right.right = Node(7)
"""
SPECS:
print(tree.get_boustrophedon())
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""

0 comments on commit 9c355b0

Please sign in to comment.