You are given two arrays prices and actions of equal length that describe a sequence of stock transactions, and an integer tax_rate (a percentage from 0 to 100). For each index i, actions[i] is either "buy" or "sell" and prices[i] is the price at which that transaction happens.
Shares are matched in FIFO order: every "sell" sells exactly one share, matched against the earliest unmatched "buy". If a sell happens with no earlier unmatched buy, ignore that sell.
For each matched buy-sell pair, the profit is sell_price - buy_price. If the profit is positive, the tax owed on that pair is floor(profit * tax_rate / 100). A loss or a zero profit owes no tax.
Return the total tax owed across all matched pairs.
Function Signature
def calculate_tax(prices: List[int], actions: List[str], tax_rate: int) -> int:
Rules
-
Transactions are processed in index order.
-
Each buy adds one share and each sell removes exactly one share.
-
A sell is matched against the earliest buy that has not been matched yet. A sell with no unmatched buy available is skipped; it is not kept for a later buy.
-
Tax is floored for each pair separately, and the per-pair amounts are summed.
-
Buys that are never matched owe no tax.
Constraints
-
1 <= len(prices) <= 10^5
-
len(prices) == len(actions)
-
0 <= prices[i] <= 10^4
-
actions[i]
is either
"buy"
or
"sell"
-
0 <= tax_rate <= 100
Examples
Input: prices = [10, 15, 20, 12], actions = ["buy", "sell", "buy", "sell"], tax_rate = 20
Output: 1
The buy at 10 is matched with the sell at 15 for a profit of 5, and floor(5 * 20 / 100) = 1. The buy at 20 is matched with the sell at 12, which is a loss, so that pair owes nothing.
Input: prices = [100, 90, 80], actions = ["buy", "buy", "sell"], tax_rate = 25
Output: 0
The sell at 80 is matched with the earliest buy, at 100, which is a loss. The buy at 90 stays unmatched, so no tax is owed.
Input: prices = [5, 10, 30, 25, 35], actions = ["sell", "buy", "buy", "sell", "sell"], tax_rate = 30
Output: 5
The first sell has no buy before it, so it is ignored. The sell at 25 matches the buy at 10 (profit 15, tax floor(4.5) = 4) and the sell at 35 matches the buy at 30 (profit 5, tax floor(1.5) = 1), for a total of 5. Flooring the combined profit of 20 once would give 6, which is wrong.