Ctrl K

Orders of Identical Parts

Hard
A warehouse stores parts, where items[i] is the part number of the i-th part; parts with the same number are identical. Customer orders are given in orders: the j-th customer wants orders[j] parts. Can every order be filled at once, given that: - the j-th customer gets exactly orders[j] parts; - all parts in one order share the same part number; - each part goes to at most one customer? Leftover parts may stay in the warehouse. Return True if this is possible and False otherwise.
Example 1
items=[7, 7, 7, 2, 2, 5], orders=[2, 3] → True

The order for 3 takes the three 7s, the order for 2 takes the two 2s.

Example 2
items=[1, 1, 2, 2, 3, 3], orders=[3] → False

No kind has three pieces, and an order cannot mix kinds.

Example 3
items=[4, 4, 4, 4, 4], orders=[2, 2] → True
Constraints
  • 1 ≤ len(items) ≤ 10⁵
  • 1 ≤ items[i] ≤ 1000
  • At most 50 distinct part numbers
  • 1 ≤ len(orders) ≤ 10
  • 1 ≤ orders[j] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSettle Up in the Fewest Transfers HardSquare-Sum Chains HardBest Word Set From Tiles HardSharing Jobs Fairly HardSquare Frame From Sticks Medium
loading Python…
Input
items=[7, 7, 7, 2, 2, 5], orders=[2, 3]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 65 tests on the server, hidden ones included.