Engineering platform~10 min2s · 256 MBtarget O(n) time, O(1) spaceclassic twin: lc-485traversal
Your team's build system records each build of a branch as a pass or a fail, in the order the builds finished. A branch is stable when many builds pass in a row.
Your team lead says: "Show the longest green streak: the most builds in a row that all passed."
A failed build ends a streak. The next passing build starts a new one.
Write longest_green_streak(results).
results[i] is 1 if build i passed and 0 if it failed.Input: results = [1, 1, 0, 1, 1, 1, 0, 1]
Output: 3A streak grows on each pass and resets on a failure.
| Build | Result | Current streak | Best so far |
|---|---|---|---|
| 0 | pass | 1 | 1 |
| 1 | pass | 2 | 2 |
| 2 | fail | 0 | 2 |
| 3 | pass | 1 | 2 |
| 4 | pass | 2 | 2 |
| 5 | pass | 3 | 3 |
| 6 | fail | 0 | 3 |
| 7 | pass | 1 | 3 |
The longest streak is 3.
Input: results = [0, 0, 0]
Output: 0No build passed, so there is no streak.
Input: results = [1, 1, 1, 1]
Output: 4Every build passed, so the streak covers the whole list.
1 ≤ results.length ≤ 100,000 (builds)results[i] is 0 or 1Walking forward from every build costs up to n(n + 1) / 2 steps, which is 5,000,050,000 when every build passes. A method that reads each build once makes 100,000 steps.