Shipment Package Status
MediumHash Map · Walmart
An online order can ship in several packages. You are given:
packages: a list of[order_id, package_id]. Each package belongs to exactly one order, and every order has at least one package.events: a list of[package_id, status, ts]tracking updates, wherestatusis"SHIPPED","DELIVERED", or"CANCELLED"andtsis an integer timestamp. Events are not sorted by time.
A package's status is the status of its event with the largest ts; if several events share that largest ts, the one appearing last in events wins. A package with no events is "PENDING". Events for unknown package ids are ignored.
An order's status is determined from its packages' statuses:
"CANCELLED"if every package is cancelled.- Otherwise, ignore the cancelled packages. If all remaining packages have the same status, the order has that status (
"PENDING","SHIPPED", or"DELIVERED"). - Otherwise the order is
"PARTIAL".
Return a list of [order_id, status] rows, one per order, sorted by order_id ascending.
Example 1
- Input:
- packages = [["o1", "p1"], ["o1", "p2"], ["o2", "p3"], ["o3", "p4"], ["o3", "p5"]], events = [["p1", "SHIPPED", 1], ["p1", "DELIVERED", 5], ["p2", "CANCELLED", 3], ["p3", "DELIVERED", 4], ["p3", "SHIPPED", 2], ["p4", "SHIPPED", 2]]
- Output:
- [["o1", "DELIVERED"], ["o2", "DELIVERED"], ["o3", "PARTIAL"]]
- Explanation
- In
o1,p2is cancelled and ignored, andp1ended delivered. Forp3the event atts = 4is the latest even though it is listed first. Ino3,p4is shipped butp5has no events and is pending, so the order is partial.
Example 2
- Input:
- packages = [["a", "x"], ["a", "y"], ["b", "z"]], events = [["x", "CANCELLED", 1], ["y", "CANCELLED", 2], ["q", "DELIVERED", 3]]
- Output:
- [["a", "CANCELLED"], ["b", "PENDING"]]
- Explanation
- Both packages of
aare cancelled.zhas no events, sobis pending. The event for unknown packageqis ignored.
Example 3
- Input:
- packages = [["o", "p"]], events = [["p", "SHIPPED", 3], ["p", "DELIVERED", 3]]
- Output:
- [["o", "DELIVERED"]]
- Explanation
- Both events have
ts = 3, so the later one in the list,DELIVERED, decides the package status.
Constraints
1 <= packages.length <= 10^5,0 <= events.length <= 10^5- Package ids are distinct; order and package ids are non-empty strings
statusis"SHIPPED","DELIVERED", or"CANCELLED", and0 <= ts <= 10^9