Problem · Array

Can Every Package Fit in a Box

Learn this problem
EasyIMCINTERNOA

Problem statement

You are given two integer arrays:

  • packages, where packages[i] is the size of the i-th package.
  • boxes, where boxes[j] is the capacity of the j-th box.

Each package must be placed into a distinct box, and each box can contain at most one package. A package of size p can fit into a box of capacity b when p <= b.

Return true if it is possible to assign every package to a different box so that all packages fit. Otherwise, return false.

Function

canFitPackages(packages: int[], boxes: int[]) → boolean

Examples

Example 1

packages = [2, 3, 5]boxes = [3, 5, 6, 2]return = true

The packages of sizes 2, 3, and 5 can be assigned to boxes with capacities 2, 3, and 5.

Example 2

packages = [2, 3, 5]boxes = [5, 3]return = false

There are only two boxes but three packages, so at least one package cannot be assigned.

Constraints

  • 1 <= packages.length, boxes.length <= 2 * 10^5
  • 1 <= packages[i], boxes[j] <= 10^9

More IMC problems

drafts saved locally
public boolean canFitPackages(int[] packages, int[] boxes) {
  // write your code here
}
packages[2, 3, 5]
boxes[3, 5, 6, 2]
expectedtrue
checking account