← 返回 doordash 的题目列表Validate Shopping Cart
类型:online_judge
Problem: Validate Shopping Cart
You are implementing shopping cart validation for a food delivery platform. Given a product catalog and a user's cart, determine whether the cart is valid and output all invalid items with their error reasons.
Each catalog item contains:
item_id: item ID, a string
available: currently available inventory
min_limit: minimum quantity allowed for this item in one order
max_limit: maximum quantity allowed for this item in one order
Each cart line contains:
item_id: item ID
quantity: quantity requested by the user
Validation Rules
If a cart item does not exist in the catalog, its error reason is UNKNOWN_ITEM.
If a cart line has quantity <= 0, its item has error reason INVALID_QUANTITY.
The same item_id may appear multiple times in the cart:
If all quantities for that item are valid, aggregate them first and then run the remaining validations.
If any line for that item has an invalid quantity, output INVALID_QUANTITY for that item and skip min/max/availability checks for it.
For an item that exists in the catalog and has valid quantities, let total_quantity be its aggregated quantity:
If total_quantity < min_limit, the error reason is BELOW_MIN.
If total_quantity > max_limit, the error reason is ABOVE_MAX.
If total_quantity > available, the error reason is OUT_OF_STOCK.
An item may have multiple error reasons; output all of them.
If the entire cart is valid, output VALID.
If there are errors, output invalid items in lexicographical order of item_id. Each line should have the format:
item_id: ERROR1,ERROR2,...
The fixed error ordering is:
UNKNOWN_ITEM, INVALID_QUANTITY, BELOW_MIN, ABOVE_MAX, OUT_OF_STOCK
Input Format
n m
item_id available min_limit max_limit
... n lines
item_id quantity
... m lines
Output Format
If the cart is valid:
VALID
Otherwise, output each invalid item and its error reasons.
Constraints
1 <= n <= 2 * 10^5
0 <= m <= 2 * 10^5
1 <= len(item_id) <= 30
0 <= available <= 10^9
1 <= min_limit <= max_limit <= 10^9
-10^9 <= quantity <= 10^9
Example
Input:
3 2
burger 10 1 5
fries 20 1 10
soda 0 1 2
burger 2
fries 5
Output:
VALID
Example
Input
3 2
burger 10 1 5
fries 20 1 10
soda 0 1 2
burger 2
fries 5
Output
VALID