Find the fewest partitions that can hold all stored data

Quick Overview

Given each partition's capacity and the amount of data it currently stores, find the smallest number of partitions that can hold all the data once it can be moved freely. Tests greedy selection by largest capacity, a short exchange argument, and the empty-data edge case.

Find the fewest partitions that can hold all stored data

Company: Cloudflare

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given two integer arrays `capacity` and `used`, both of length `n`. Partition `i` can hold `capacity[i]` units of data and currently stores `used[i]` units. Data can be moved between partitions freely, in chunks of any size. Return the minimum number of partitions needed to store all of the data currently spread across the partitions. Put another way, find the size of the smallest subset of partitions whose total capacity is at least the total amount of data stored across all partitions. ### Function Signature ```python def min_partitions(capacity: list[int], used: list[int]) -> int: ``` ### Rules - `capacity[i]` and `used[i]` describe the same partition `i`. - A partition you keep can hold data up to its full capacity, whatever it stored before the move. - If the total amount of stored data is 0, return 0. ### Constraints - `1 <= n <= 10^5` - `1 <= capacity[i] <= 10^9` - `0 <= used[i] <= capacity[i]` ### Examples ```text Input: capacity = [10, 20, 30, 40], used = [5, 10, 15, 20] Output: 2 ``` The total data is 5 + 10 + 15 + 20 = 50. The largest single partition holds only 40, but the two largest hold 40 + 30 = 70, which is enough. ```text Input: capacity = [5, 5, 5], used = [3, 3, 3] Output: 2 ``` The total data is 9. One partition holds 5, which is too little, and any two partitions hold 10. ```text Input: capacity = [3, 8], used = [0, 0] Output: 0 ``` No data is stored, so no partition is needed.

Overview: Given each partition's capacity and the amount of data it currently stores, find the smallest number of partitions that can hold all the data once it can be moved freely. Tests greedy selection by largest capacity, a short exchange argument, and the empty-data edge case.

Cloudflare logo
Cloudflare
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given two integer arrays capacity and used, both of length n. Partition i can hold capacity[i] units of data and currently stores used[i] units.

Data can be moved between partitions freely, in chunks of any size. Return the minimum number of partitions needed to store all of the data currently spread across the partitions.

Put another way, find the size of the smallest subset of partitions whose total capacity is at least the total amount of data stored across all partitions.

Function Signature

def min_partitions(capacity: list[int], used: list[int]) -> int:

Rules

  • capacity[i] and used[i] describe the same partition i .
  • A partition you keep can hold data up to its full capacity, whatever it stored before the move.
  • If the total amount of stored data is 0, return 0.

Constraints

  • 1 <= n <= 10^5
  • 1 <= capacity[i] <= 10^9
  • 0 <= used[i] <= capacity[i]

Examples

Input: capacity = [10, 20, 30, 40], used = [5, 10, 15, 20]
Output: 2

The total data is 5 + 10 + 15 + 20 = 50. The largest single partition holds only 40, but the two largest hold 40 + 30 = 70, which is enough.

Input: capacity = [5, 5, 5], used = [3, 3, 3]
Output: 2

The total data is 9. One partition holds 5, which is too little, and any two partitions hold 10.

Input: capacity = [3, 8], used = [0, 0]
Output: 0

No data is stored, so no partition is needed.