Skip to content

Commit b123ad4

Browse files
authored
Merge pull request #1547 from ivanpenaloza/february13
adding algo
2 parents 79b1392 + 723311c commit b123ad4

5 files changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
23+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
class Solution:
11+
def maxPathSum(self, root: Optional[TreeNode]) -> int:
12+
self.max_sum = float('-inf')
13+
14+
def dfs(node: Optional[TreeNode]) -> int:
15+
"""
16+
Returns the maximum path sum that can be extended to the parent.
17+
Updates self.max_sum with the maximum path sum through this node.
18+
"""
19+
if not node:
20+
return 0
21+
22+
# Get maximum contribution from left and right subtrees
23+
# Use max with 0 to ignore negative paths
24+
left_max = max(0, dfs(node.left))
25+
right_max = max(0, dfs(node.right))
26+
27+
# Maximum path sum through this node (can't be extended upward)
28+
path_through_node = node.val + left_max + right_max
29+
30+
# Update global maximum
31+
self.max_sum = max(self.max_sum, path_through_node)
32+
33+
# Return maximum path that can be extended to parent
34+
# Can only choose one direction (left or right)
35+
return node.val + max(left_max, right_max)
36+
37+
dfs(root)
38+
return self.max_sum
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
function maxPathSum(root: TreeNode | null): number {
4+
let maxSum = -Infinity;
5+
6+
function dfs(node: TreeNode | null): number {
7+
/**
8+
* Returns the maximum path sum that can be extended to the parent.
9+
* Updates maxSum with the maximum path sum through this node.
10+
*/
11+
if (!node) {
12+
return 0;
13+
}
14+
15+
// Get maximum contribution from left and right subtrees
16+
// Use Math.max with 0 to ignore negative paths
17+
const leftMax = Math.max(0, dfs(node.left));
18+
const rightMax = Math.max(0, dfs(node.right));
19+
20+
// Maximum path sum through this node (can't be extended upward)
21+
const pathThroughNode = node.val + leftMax + rightMax;
22+
23+
// Update global maximum
24+
maxSum = Math.max(maxSum, pathThroughNode);
25+
26+
// Return maximum path that can be extended to parent
27+
// Can only choose one direction (left or right)
28+
return node.val + Math.max(leftMax, rightMax);
29+
}
30+
31+
dfs(root);
32+
return maxSum;
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
from typing import Optional, List
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.ex_75_binary_tree_maximum_path_sum import Solution, TreeNode
5+
6+
7+
class BinaryTreeMaximumPathSumTestCase(unittest.TestCase):
8+
9+
def test_example_1(self):
10+
"""
11+
Input: root = [1,2,3]
12+
Output: 6
13+
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
14+
"""
15+
solution = Solution()
16+
tree = TreeNode(1, TreeNode(2), TreeNode(3))
17+
output = solution.maxPathSum(root=tree)
18+
self.assertEqual(output, 6)
19+
20+
def test_example_2(self):
21+
"""
22+
Input: root = [-10,9,20,null,null,15,7]
23+
Output: 42
24+
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
25+
"""
26+
solution = Solution()
27+
tree = TreeNode(-10)
28+
tree.left = TreeNode(9)
29+
tree.right = TreeNode(20)
30+
tree.right.left = TreeNode(15)
31+
tree.right.right = TreeNode(7)
32+
output = solution.maxPathSum(root=tree)
33+
self.assertEqual(output, 42)

0 commit comments

Comments
 (0)