/53. Maximum Subarray

53. Maximum Subarray

Medium
Arrays53.1% acceptance

Given an integer array arr, return the maximum sum obtainable from any contiguous subarray (of at least one element) within arr.

Example 1

Input: [2, -1, 3, -4, 5, -2, 6]

Output: 12

Explanation: The subarray [2, -1, 3, -4, 5, -2, 6] has the largest sum 12.

Example 2

Input: [-5, -2, -3, -1]

Output: -1

Explanation: The subarray [-1] has the largest sum -1.

Example 3

Input: [7]

Output: 7

Explanation: The subarray [7] has the largest sum 7.

Constraints

  • 1 <= len(arr) <= 105
  • -104 <= arr[i] <= 104 for all i
Python (current runtime)

Case 1

Input: [0, -2, 3, 5, -1, 2]

Expected: 9

Case 2

Input: [-10, 2, 3, -2, 4, -1]

Expected: 7

Case 3

Input: [1, 2, 3, 4, 5]

Expected: 15

Case 4

Input: [-3, -2, -1, -4]

Expected: -1

Case 5

Input: [4, -1, 2, 1, -5, 4]

Expected: 6