diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/longest_common_subsequence.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/longest_common_subsequence.py new file mode 100644 index 00000000..d5d4d0af --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/longest_common_subsequence.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional + +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + + # Make a grid of 0's with len(text2) + 1 columns + # and len(text1) + 1 rows. + len_1 = len(text1) + len_2 = len(text2) + dp_grid = [[0]*(len_2+1) for _ in range(len_1+1)] + + # Iterate up each column, starting from the last one. + for j in reversed(range(len_2)): + for i in reversed(range(len_1)): + if text1[i] == text2[j]: + dp_grid[i][j] = dp_grid[i+1][j+1] + 1 + else: + dp_grid[i][j] = max(dp_grid[i+1][j], dp_grid[i][j+1]) + + # The original problem's answer is in dp_grid[0][0]. Return it. + return dp_grid[0][0] + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/lowest_common_ancestor_binary_tree_iv.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/lowest_common_ancestor_binary_tree_iv.py new file mode 100644 index 00000000..71d4191d --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/lowest_common_ancestor_binary_tree_iv.py @@ -0,0 +1,29 @@ +from typing import List, Union, Collection, Mapping, Optional + +# Definition for a binary tree node. +class TreeNode: + def __init__(self, x): + self.val = x + self.left = None + self.right = None + +class Solution: + def lowestCommonAncestor(self, root: TreeNode, nodes: List[TreeNode]) -> 'TreeNode': + + node_set = set(nodes) + + def dfs(node: TreeNode): + + if not node or node in node_set: + return node + + left = dfs(node.left) + right = dfs(node.right) + + if left and right: + return node + + return left if left else right + + return dfs(root) + \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/best_time_to_sell_stocks_i.py b/src/my_project/interviews/top_150_questions_round_22/best_time_to_sell_stocks_i.py new file mode 100644 index 00000000..c863b07d --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/best_time_to_sell_stocks_i.py @@ -0,0 +1,19 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC +import math + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + + min_price = math.inf + max_profit = 0 + len_prices = len(prices) + + for i in range(len_prices): + + if prices[i] < min_price: + min_price = prices[i] + elif prices[i] - min_price > max_profit: + max_profit = prices[i] - min_price + + return max_profit \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/best_time_to_sell_stocks_ii.py b/src/my_project/interviews/top_150_questions_round_22/best_time_to_sell_stocks_ii.py new file mode 100644 index 00000000..0de5446b --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/best_time_to_sell_stocks_ii.py @@ -0,0 +1,25 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + + i = 0 + peak = None + valley = None + len_prices = len(prices) + result = 0 + + while i < len(prices) - 1: + + while i < len_prices - 1 and prices[i] >= prices[i+1]: + i += 1 + valley = prices[i] + + while i < len_prices - 1 and prices[i] <= prices[i+1]: + i += 1 + peak = prices[i] + + result += peak - valley + + return result diff --git a/tests/test_150_questions_round_22/test_best_time_to_sell_stocks_i_round_22.py b/tests/test_150_questions_round_22/test_best_time_to_sell_stocks_i_round_22.py new file mode 100644 index 00000000..7f97e98a --- /dev/null +++ b/tests/test_150_questions_round_22/test_best_time_to_sell_stocks_i_round_22.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.best_time_to_sell_stocks_i import Solution + +class BestTimeToSellStockTestCase(unittest.TestCase): + + def test_best_time_to_sell_stock(self): + solution = Solution() + output = solution.maxProfit(prices=[7,1,5,3,6,4]) + target = 5 + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_best_time_to_sell_stocks_ii_round_22.py b/tests/test_150_questions_round_22/test_best_time_to_sell_stocks_ii_round_22.py new file mode 100644 index 00000000..d1370fb1 --- /dev/null +++ b/tests/test_150_questions_round_22/test_best_time_to_sell_stocks_ii_round_22.py @@ -0,0 +1,13 @@ +import unittest +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.best_time_to_sell_stocks_ii import Solution + + +class BestTimeToSellStockTestCase(unittest.TestCase): + + def test_best_time_to_sell_stock(self): + solution = Solution() + output = solution.maxProfit(prices=[7,1,5,3,6,4]) + target = 7 + self.assertEqual(output, target) \ No newline at end of file