Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 []
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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)