All problems
0140MediumArrayBinary SearchSliding WindowPrefix Sum

Fewest Shifts to Clear the Quota

Tracked in this browser only
Write code

Trains the technique from

LeetCode 209Minimum Size Subarray Sum

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A field engineer logs the hours worked on each day of a tour, day by day, in hours.

Payroll wants to credit one unbroken run of days whose logged hours add up to quota or more, and it wants that run to cover as few days as it can. The run has to be consecutive days: skipping a day inside it is not allowed.

Return the number of days in the shortest qualifying run. When no run of consecutive days reaches quota, return 0.

Examples

Example 1

Input
quota = 11, hours = [4, 2, 5, 1, 6, 3]
Output
3

The days logging 5, 1 and 6 add up to 12, which clears the quota with three days. No pair of neighbouring days gets past 9, so two days are never enough.

Example 2

Input
quota = 10, hours = [1, 1, 10]
Output
1

The final day alone logs 10 and clears the quota, so a single day is the shortest run even though the whole tour also qualifies.

Example 3

Input
quota = 100, hours = [1, 2, 3]
Output
0

The entire tour logs only 6 hours, so no run can reach 100 and the answer falls back to 0.

Constraints

  • 1 <= quota <= 10^9
  • 1 <= hours.length <= 10^5
  • 1 <= hours[i] <= 10^4
  • Return 0 when no run of consecutive days reaches quota

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def fewest_shifts_for_quota(quota: int, hours: list[int]) -> int:
Java
public int fewestShiftsForQuota(int quota, int[] hours)
September 7
Apply