Skip to the code
cf.
+25 XP

#135 Longest Run Within a Byte Budget

medium

A logging library batches log lines before sending them. Line i takes sizes[i] bytes. A batch must be a run of neighboring lines whose total size is at most budget bytes.

Write longestBatch(sizes, budget) that returns the largest number of neighboring lines that fit in one batch. If no single line fits, return 0.

Example 1

Input: sizes = [2,1,5,1,3,2], budget = 7
Output: 3

[1, 5, 1] totals 7, and no run of 4 lines fits.

Example 2

Input: sizes = [9,8], budget = 5
Output: 0

Every line alone is bigger than the budget.

Constraints

  • 0 <= sizes.length <= 100000
  • 0 <= sizes[i] <= 10^4
  • 0 <= budget <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).