Maximum Subarray
Real-world version: Best Trading Day RunThe statement here is our own: the same inputs, outputs and technique.
A market stall balances its books every evening. daily[i] is the money the stall cleared on day i: a positive figure on a day it came out ahead, a negative figure on a day it went backwards, and 0 on a day that broke even.
The owner wants to quote her strongest run of trading. A run is the days from some day i through some day j with i <= j, taken back to back with no days left out in between, and its worth is the sum of the figures on those days. Return the worth of the strongest run in daily.
A run must contain at least one day; skipping every day is not allowed. So when the stall went backwards on all of its days, the strongest run is the single day it lost the least on, and the answer you return is negative. Do not report 0 for that case.
Examples
Example 1
- Input
- daily = [3, -2, 7, -8, 4]
- Output
- 8
Days 0 through 2 clear 3 - 2 + 7 = 8. Taking day 2 by itself yields only 7, and carrying on into day 3 gives back 8 of it, so no other run beats 8.
Example 2
- Input
- daily = [-6, -2, -9]
- Output
- -2
Every run of two or more days is worse than its own worst day, so the strongest choice is day 1 alone at -2. Since a run cannot be empty, a negative answer is the correct one.
Example 3
- Input
- daily = [5]
- Output
- 5
One day of trading means exactly one run is available and its worth is 5.
Constraints
- 1 <= daily.length <= 10^5
- -10^4 <= daily[i] <= 10^4
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def best_run_total(daily: list[int]) -> int:public int bestRunTotal(int[] daily)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Maximum Sum Circular SubarrayReal-world version: Best Arc of the Ring RoadMedium
- Longest Increasing Subsequence IIReal-world version: Longest Rising Run Under a Step CapHard
- Best Time to Buy and Sell StockReal-world version: One Flip on the Card MarketEasy
- Majority ElementReal-world version: Quorum Drift ValueEasy