Skip to the code
+25 XP

#102 Two-File Upload Batches

medium

A file uploader sends files in batches. Each batch can hold at most two files, and the total size of a batch can't exceed limit megabytes. Every single file is at most limit.

Write fewestBatches(sizes, limit) that returns the smallest number of batches needed to upload every file.

Example 1

Input: sizes = [3,5,3,4], limit = 5
Output: 4

No two files fit together: the smallest pair, 3 + 3, is 6. So each file needs its own batch.

Example 2

Input: sizes = [1,2,2,3], limit = 4
Output: 2

Batch 1 + 3 and 2 + 2.

Constraints

  • 0 <= sizes.length <= 200000
  • 1 <= sizes[i] <= limit <= 30000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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