Problem · Array
Can Every Package Fit in a Box
Learn this problemProblem statement
You are given two integer arrays:
packages, wherepackages[i]is the size of thei-th package.boxes, whereboxes[j]is the capacity of thej-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[]) → booleanExamples
Example 1
packages = [2, 3, 5]boxes = [3, 5, 6, 2]return = trueThe 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 = falseThere are only two boxes but three packages, so at least one package cannot be assigned.
Constraints
1 <= packages.length, boxes.length <= 2 * 10^51 <= packages[i], boxes[j] <= 10^9