Fractional Stock Inventory
You are given two lists that are both sorted by stock symbol: a list of pending orders and a list representing your current fractional stock holdings. Write a function solve(inventory, orders) that processes each order in sequence and returns the updated inventory after all orders have been handled, keeping the result sorted by symbol.
Each order is an array of the form [symbol, type, quantity, unit_price]:
symbol: a ticker string (e.g., "AAPL").type: "B" for a buy order, "S" for a sell order.quantity: either a plain integer (representing a share fraction in hundredths) or a dollar amount string starting with "$" (also in hundredths).unit_price: the current price of one share, expressed in hundredths.Each inventory entry is a pair [symbol, amount] where amount is an integer between 0 and 99 inclusive, representing the current fractional share count in hundredths. For example, an inventory entry ["AAPL", 50] means you hold 0.50 shares of AAPL.
Important: the last two digits of every integer are the decimal part. For instance, an order ["AAPL", "B", "42", "100"] means you want to buy 0.42 shares of AAPL at $1.00 per share. A dollar-based order like ["AAPL", "B", "$50", "100"] means you want to spend $0.50; at $1.00 per share this buys 0.50 shares. An inventory value of 50 means 0.50 shares.
Your inventory always holds strictly less than one whole share of any stock. This invariant must be maintained after each order is processed: if the fraction reaches 100 or more, discard the whole‑share part and keep only the remainder modulo 100.
Processing rules:
Buy order:
If the current fractional inventory for that symbol is sufficient to cover the demanded quantity, simply subtract the order amount from the inventory. If the inventory is insufficient, calculate the shortfall, purchase enough whole shares to cover it, subtract the ordered quantity, and keep whatever remains as the new fractional inventory (which will be less than 100).
Sell order:
Add the sold quantity to the existing fractional inventory. Then remove any whole shares by taking the result modulo 100, leaving only the fractional part.
When the order quantity is a dollar amount, first convert it to a share quantity by dividing the dollar amount by the unit price (both in hundredths). Use integer arithmetic carefully to preserve the conversion.
After processing all orders, return the inventory as a list of [symbol, amount] pairs, sorted by symbol.
Example 1:
Input: orders = [["AAPL", "B", "42", "100"], ["GOOG", "S", "$80", "160"]], inventory = [["AAPL", "99"], ["GOOG", "60"]]
Output: [["AAPL", "57"], ["GOOG", "10"]]
Explanation:
inventory = [['AAPL','99'], ['GOOG', '60']] orders = [['AAPL', 'B', '42', '100'], ['GOOG', 'S', '$80', '160']]
[['AAPL','57'], ['GOOG', '10']]
| 0 | 1 | |
|---|---|---|
| 0 | AAPL | 99 |
| 1 | GOOG | 60 |
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0 | AAPL | B | 42 | 100 |
| 1 | GOOG | S | $80 | 160 |
We start with the initial inventory and two pending orders.
Example 2:
Input: orders = [["AAPL","B","$42","100"]], inventory = [["AAPL","50"]]
Output: [["AAPL","8"]]
Example 3:
Input: orders = [["AAPL","S","75","100"]], inventory = [["AAPL","60"]]
Output: [["AAPL","35"]]
Constraints:
[0, 99]."$42" (also in hundredths).amount < 100).inventory = [['AAPL','99'], ['GOOG', '60']] orders = [['AAPL', 'B', '42', '100'], ['GOOG', 'S', '$80', '160']]
[['AAPL','57'], ['GOOG', '10']]
| 0 | 1 | |
|---|---|---|
| 0 | AAPL | 99 |
| 1 | GOOG | 60 |
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0 | AAPL | B | 42 | 100 |
| 1 | GOOG | S | $80 | 160 |
We start with the initial inventory and two pending orders.