A marketplace trading system enables buyers to purchase items from sellers at competitive prices. The system maintains an inventory where multiple sellers may list the same item at different prices. When a buyer submits an order for an item with a maximum price they're willing to pay, the system matches them with the cheapest available listing that meets their budget.
Implement the TradingSystem class:
-
TradingSystem(String[][] items)Initializes the trading system with marketplace inventory. Each row represents one item listing and contains[itemName, seller, price, buyer], wherebuyeris an empty string if unsold; non-empty indicates already sold and unavailable. -
List<String> processBuyOrders(String[][] buyOrders)Processes a batch of buy orders. Each row represents one buy order for a single item and contains[buyerName, itemName, maxPrice]. EachitemNameappears at most once in a single call. Returns a list where each successful purchase is encoded as"itemName:price". A purchase succeeds only if the cheapest available item has price less than or equal tomaxPrice. -
String[][] getAllItems()Returns the current state of all items as a 2D array where each row is[itemName, seller, price, buyer], reflecting any buyer updates made byprocessBuyOrders.
Constraints:
- 1 ≤
items.length≤ 104 - 1 ≤
price≤ 109 itemNamevalues are non-empty strings- Multiple items can share the same
itemName(different sellers, different prices) - An item is available only if its
buyerfield is an empty string - When multiple available items share the same
itemName, always match with the cheapest one
Example:
Input: ["TradingSystem", "processBuyOrders", "getAllItems"] [[[["book", "Alice", "100", ""], ["book", "Bob", "120", ""], ["pen", "Carol", "50", ""], ["book", "David", "90", ""]]], [[["Charlie", "book", "110"], ["Eve", "pen", "40"]]], []]
Output: [null, ["book:90"], [["book", "Alice", "100", ""], ["book", "Bob", "120", ""], ["pen", "Carol", "50", ""], ["book", "David", "90", "Charlie"]]]
Explanation:
TradingSystem system = new TradingSystem(items);// Initialize with four items: three books at prices 100 (Alice), 120 (Bob), and 90 (David), plus one pen at price 50 (Carol). All items are initially unsold.system.processBuyOrders([["Charlie", "book", "110"], ["Eve", "pen", "40"]]);// Returns ["book:90"]. Charlie requests a book with maxPrice 110. The system identifies three available books with prices 100, 120, and 90. It selects David's book at price 90 (the cheapest), which is within Charlie's budget. The purchase succeeds and David's book buyer field is updated to "Charlie". Eve requests a pen with maxPrice 40. The only available pen costs 50, which exceeds Eve's budget, so no transaction occurs.system.getAllItems();// Returns [["book", "Alice", "100", ""], ["book", "Bob", "120", ""], ["pen", "Carol", "50", ""], ["book", "David", "90", "Charlie"]]. David's book now has buyer="Charlie"; all other items remain unsold.
