You have a list of orders and an initial inventory of stock fractions, both sorted by stock symbol. Each order is given as a list in the format [<symbol>, <order_type>, <quantity>, <unit_price>] where:
symbolis the stock symbol like "AAPL".order_type"is an order type, with"B"for buy and"S"for sell.quantitycan be either a number representing a fraction of a share or a dollar amount prefixed with"$".unit_priceis the current stock price in hundredths.
The inventory is a list where each item consists of a stock symbol and the corresponding fraction of a share (expressed in hundredths).
Note: The final two digits of all numbers represent the decimal part. For example, an order
["AAPL", "B", "42", "100"]indicates a buy order for 0.42 shares of "AAPL", where "AAPL" is priced at $1.00 per share. Similarly,["AAPL", "B", "$50", "100"]means to purchase shares worth $0.50, which equates to buying 0.50 shares at $1.00 per share. An inventory entry like["AAPL", "50"]means you start with 0.50 shares of "AAPL".
Each stock’s inventory is strictly a fractional share. If, after processing orders, the share count exceeds 1, only the fractional part is kept (the whole number portion is removed). The task is to process each order sequentially and then return the updated fractional inventory, sorted by symbol. The rules for processing orders are as follows:
-
Buy orders:
If the current inventory holds enough fractional shares to cover the buy order, simply subtract the order amount from the inventory. If not, determine the shortfall and purchase enough whole shares to cover it. After the purchase, subtract the order amount and keep any leftover as the new fractional inventory. -
Sell orders:
Add the sold fraction to the inventory, then adjust the total by removing any whole shares, keeping only the fractional part to ensure the inventory remains less than one whole share.
Example 1:
Input: orders = [["AAPL", "B", "42", "100"], ["GOOG", "S", "$80", "160"]], inventory = [["AAPL", "99"], ["GOOG", "60"]] Output: [["AAPL", "57"], ["GOOG", "10"]] Explanation:
- For the "AAPL" order:
- It is a buy order with a quantity of 42, which represents 0.42 shares (since values are in hundredths).
- The initial inventory for AAPL is 99 (i.e., 0.99 shares).
- Since 0.99 shares are sufficient to cover the 0.42 shares order, we subtract 42 from 99, resulting in 57 (i.e., 0.57 shares remaining).
- For the "GOOG" order:
- It is a sell order worth $80. For dollar-based orders, convert the dollar amount to shares using the current price: $0.80 / $1.60 = 0.5 shares
- The initial inventory for GOOG is 60 (i.e., 0.6 shares).
- For a sell order, we add the calculated order shares to the inventory: 60 + 50 = 110.
- Finally, we “flatten” the inventory by taking the modulo 100, which leads to 10 in the end.
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"]]
