leetle

Daily challenge for 2026-10-21

659. File Backup Batch Fitting

You are given a list of positive integers sizes representing the sizes of incoming files in sequential order, and an array of integers capacities representing different target storage capacities.

For each capacity, determine the maximum number of leading files from the beginning of the list that can fit entirely into storage without reordering. That is, find the length of the longest prefix of sizes whose total sum does not exceed the given capacity.

Return an array of integers containing the count of files for each capacity query in capacities.

Constraints:
There are 0–2,000 file sizes and 0–2,000 capacity queries. File sizes are positive integers at most 1,000,000; capacities are non-negative integers at most 1,000,000,000. Query order must be preserved.

Example:
Input: sizes = [2, 3, 5, 1], capacities = [1, 2, 6, 12]
Output: [0, 1, 2, 4]

Run this challenge interactively in Python or JavaScript when the app loads.