Skip to the code
cf.
+25 XP

#136 Shortest Burst Reaching a Goal

medium

A flash sale dashboard records how many orders came in each second in orders. The team wants to know the shortest burst of consecutive seconds that brought in at least goal orders.

Write shortestBurst(orders, goal) that returns the length of the shortest run of neighboring seconds whose total is at least goal. If no run reaches the goal, return 0.

Example 1

Input: orders = [2,3,1,2,4,3], goal = 7
Output: 2

[4, 3] reaches 7 in two seconds.

Example 2

Input: orders = [1,1,1], goal = 5
Output: 0

All three seconds together only make 3 orders.

Constraints

  • 0 <= orders.length <= 100000
  • 1 <= orders[i] <= 10^4
  • 1 <= goal <= 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).