Problem 420891 · medium · Phase 04 Non-Linear Data Structures

Best Trading Streak by Halving

divide and conquer · recursion · arrays

A trader's daily profits and losses are recorded in days (a non-empty list of integers). A streak is one or more consecutive days. Write best_streak(days) returning the largest possible total over any streak, using a divide-and-conquer approach: split the list in half, solve each half, and combine.

Examples

Input:  days = [2, -5, 6, -1, 4, -7, 3]
Output: 9
Explanation: the streak 6, -1, 4 totals 9.

Input:  days = [-3, -1, -2]
Output: -1

Constraints

  • 1 <= len(days) <= 10**4, -10**4 <= days[i] <= 10**4
  • Target: O(n log n) time; the recursion depth is about log2(n).

Goals

  • Split an array in half and combine three candidate answers
  • Compute the best crossing sum from the midpoint outward
  • Handle all-negative input where the best streak is one element
Starting Python…