← 返回 akunacapital 的题目列表Portfolio Rebalancer (Allocation Diff)
类型:online_judge
Problem: Portfolio Rebalancer (Allocation Diff)
Implement a portfolio rebalancing function.
You are given two portfolios:
currentPortfolio: current percentage allocations per asset
targetPortfolio: target percentage allocations per asset
Each portfolio is represented as a map Map<String, Integer>: asset -> percentage.
Implement rebalancePortfolio(currentPortfolio, targetPortfolio) returning a map Map<String, Integer> where each asset maps to:
targetPercentage - currentPercentage
Meaning:
Positive: buy/increase this asset by that percentage
Negative: sell/decrease this asset by that percentage
Constraints
Both portfolios contain the same set of asset types (2 to 10).
The allocations in each portfolio sum to 100.
All percentages are positive integers.
Example
Current: {AAPL: 30, MSFT: 70} Target: {AAPL: 50, MSFT: 50}
Output: {AAPL: 20, MSFT: -20}
Example
Input
2
AAPL 30
MSFT 70
AAPL 50
MSFT 50
Output
AAPL 20
MSFT -20