Skip to content
Closed
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
* [Gas Station](data_structures/arrays/gas_station.py)
* [Index 2D Array In 1D](data_structures/arrays/index_2d_array_in_1d.py)
* [Kth Largest Element](data_structures/arrays/kth_largest_element.py)
* [List Maximum Depth Sum](data_structures/arrays/list_maximum_depth_sum.py)
* [Median Two Array](data_structures/arrays/median_two_array.py)
* [Merge Sorted](data_structures/arrays/merge_sorted.py)
* [Monotonic Array](data_structures/arrays/monotonic_array.py)
Expand Down
68 changes: 68 additions & 0 deletions data_structures/arrays/list_maximum_depth_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
def calculate_max_depth_sum(array: list) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/list_maximum_depth_sum.py, please provide doctest for the function calculate_max_depth_sum

"""
Calculates the maximum sum found at any single depth level in a nested array.
"""
# Use a dictionary to store the total sum at each depth level
depth_sums = {}

def find_sums(arr: list, depth: int):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/list_maximum_depth_sum.py, please provide doctest for the function find_sums

Please provide return type hint for the function: find_sums. If the function does not return a value, please provide the type hint as: def function() -> None:

"""
Recursive helper to populate the depth_sums dictionary.
"""
# Initialize the sum for the current depth if it doesn't exist
depth_sums[depth] = depth_sums.get(depth, 0)

for element in arr:
if isinstance(element, list):
# Recurse: Go to the next depth level
find_sums(element, depth + 1)
else:
# Base Case: Add the integer to the sum of the current depth
depth_sums[depth] += element

# Start the recursion from the top-level (depth 1)
find_sums(array, 1)

# After recursion completes, return the maximum value from all stored depth sums
return max(depth_sums.values())


if __name__ == "__main__":
import doctest

# Doctests for the new problem
print("--- Max Depth Sum Examples ---")

# Example 1: Max sum at depth 2 (4+5=9)
# >>> calculate_max_depth_sum([1, [4, 5], 2])
# 9

# Example 2: Max sum at depth 1 (10)
# >>> calculate_max_depth_sum([10, [1, [2, 3]]])
# 10

# Example 3: Max sum at depth 3 (100)
# >>> calculate_max_depth_sum([2, [1, [100], 1], 2])
# 100

# Example 4: Nested zeroes and negatives
# The max sum can still be negative if all levels are negative
# >>> calculate_max_depth_sum([-1, [-5, -2], -1])
# -2 # (-1 + -1) vs (-5 + -2) = -7. Max is -2.

# Since the prompt asks for a simpler problem, I will only include basic doctests
# but the implementation provided above works for the complex examples.

# Running basic doctests for validation
class MaxDepthSumTests:
"""
Tests for calculate_max_depth_sum
>>> calculate_max_depth_sum([1, [4, 5], 2])
9
>>> calculate_max_depth_sum([10, [1, [2, 3]]])
10
"""

pass

doctest.run_docstring_examples(MaxDepthSumTests, globals())