From 723311c40e573bf62c7beb94b7070dc63cbaa9b2 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 13 Feb 2026 04:43:43 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_2.py | 16 ++++++++ .../common_algos/valid_palindrome_round_2.py | 23 +++++++++++ .../ex_75_binary_tree_maximum_path_sum.py | 38 +++++++++++++++++++ .../ex_75_binary_tree_maximum_path_sum.ts | 33 ++++++++++++++++ ...5_binary_tree_maximum_path_sum_round_22.py | 33 ++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_75_binary_tree_maximum_path_sum.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_75_binary_tree_maximum_path_sum.ts create mode 100644 tests/test_150_questions_round_22/test_75_binary_tree_maximum_path_sum_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py new file mode 100644 index 00000000..dd1b1232 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py @@ -0,0 +1,16 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + answer = dict() + + for k, v in enumerate(nums): + + if v in answer: + return [answer[v], k] + else: + answer[target - v] = k + + return [] diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py new file mode 100644 index 00000000..63acf7c5 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py @@ -0,0 +1,23 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # To lowercase + s = s.lower() + + # Remove non-alphanumeric characters + s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) + + # Determine if s is palindrome or not + len_s = len(s) + + for i in range(len_s//2): + + if s[i] != s[len_s - 1 - i]: + return False + + return True + diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_75_binary_tree_maximum_path_sum.py b/src/my_project/interviews/top_150_questions_round_22/ex_75_binary_tree_maximum_path_sum.py new file mode 100644 index 00000000..7aae5ad7 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_75_binary_tree_maximum_path_sum.py @@ -0,0 +1,38 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def maxPathSum(self, root: Optional[TreeNode]) -> int: + self.max_sum = float('-inf') + + def dfs(node: Optional[TreeNode]) -> int: + """ + Returns the maximum path sum that can be extended to the parent. + Updates self.max_sum with the maximum path sum through this node. + """ + if not node: + return 0 + + # Get maximum contribution from left and right subtrees + # Use max with 0 to ignore negative paths + left_max = max(0, dfs(node.left)) + right_max = max(0, dfs(node.right)) + + # Maximum path sum through this node (can't be extended upward) + path_through_node = node.val + left_max + right_max + + # Update global maximum + self.max_sum = max(self.max_sum, path_through_node) + + # Return maximum path that can be extended to parent + # Can only choose one direction (left or right) + return node.val + max(left_max, right_max) + + dfs(root) + return self.max_sum \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_75_binary_tree_maximum_path_sum.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_75_binary_tree_maximum_path_sum.ts new file mode 100644 index 00000000..d161a1e2 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_75_binary_tree_maximum_path_sum.ts @@ -0,0 +1,33 @@ +import { TreeNode } from './TreeNode'; + +function maxPathSum(root: TreeNode | null): number { + let maxSum = -Infinity; + + function dfs(node: TreeNode | null): number { + /** + * Returns the maximum path sum that can be extended to the parent. + * Updates maxSum with the maximum path sum through this node. + */ + if (!node) { + return 0; + } + + // Get maximum contribution from left and right subtrees + // Use Math.max with 0 to ignore negative paths + const leftMax = Math.max(0, dfs(node.left)); + const rightMax = Math.max(0, dfs(node.right)); + + // Maximum path sum through this node (can't be extended upward) + const pathThroughNode = node.val + leftMax + rightMax; + + // Update global maximum + maxSum = Math.max(maxSum, pathThroughNode); + + // Return maximum path that can be extended to parent + // Can only choose one direction (left or right) + return node.val + Math.max(leftMax, rightMax); + } + + dfs(root); + return maxSum; +} diff --git a/tests/test_150_questions_round_22/test_75_binary_tree_maximum_path_sum_round_22.py b/tests/test_150_questions_round_22/test_75_binary_tree_maximum_path_sum_round_22.py new file mode 100644 index 00000000..1f61ed8b --- /dev/null +++ b/tests/test_150_questions_round_22/test_75_binary_tree_maximum_path_sum_round_22.py @@ -0,0 +1,33 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_75_binary_tree_maximum_path_sum import Solution, TreeNode + + +class BinaryTreeMaximumPathSumTestCase(unittest.TestCase): + + def test_example_1(self): + """ + Input: root = [1,2,3] + Output: 6 + Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6. + """ + solution = Solution() + tree = TreeNode(1, TreeNode(2), TreeNode(3)) + output = solution.maxPathSum(root=tree) + self.assertEqual(output, 6) + + def test_example_2(self): + """ + Input: root = [-10,9,20,null,null,15,7] + Output: 42 + Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42. + """ + solution = Solution() + tree = TreeNode(-10) + tree.left = TreeNode(9) + tree.right = TreeNode(20) + tree.right.left = TreeNode(15) + tree.right.right = TreeNode(7) + output = solution.maxPathSum(root=tree) + self.assertEqual(output, 42) \ No newline at end of file