minimanimoh / Udacity_DS-A

0 stars 0 forks source link

DS&A - 2. Data Structures - Lesson 1. Max-Sum-Subarray #11

Open minimanimoh opened 3 years ago

minimanimoh commented 3 years ago

Problem Statement

You have been given an array containing numbers. Find and return the largest sum in a contiguous subarray within the input array.

Example 1:

Example 2:

def max_sum_subarray(arr):
    max_sum = arr[0]
    current_sum = arr[0]

    for num in arr[1:]:
        current_sum = max(current_sum + num, num)
        max_sum = max(current_sum, max_sum)
    return max_sum