Portfolio Multiplier Tracker
In financial modeling, accurately tracking the cumulative effect of various market factors on portfolio returns is crucial. This problem simulates a system where daily profit/loss multipliers are applied and queried over specific periods. You'll build a data structure to efficiently handle updates affecting a range of days and queries asking for the total multiplicative effect over another range.
Implement a function simulatePortfolioMultipliers that takes an initial number of days N and a list of operations. Each operation is either an update or a query.
N days, where each day initially has a multiplier of 1.0.startDay, endDay, and percentageChange, update the multiplier for all days i from startDay to endDay (inclusive). The new multiplier for each day i will be currentMultiplier_i * (1 + percentageChange / 100).queryStartDay and queryEndDay, return the product of multipliers for all days i from queryStartDay to queryEndDay (inclusive).simulatePortfolioMultipliers(numDays: number, operations: Operation[]): number[]
numDays: An integer representing the total number of days (indexed 0 to numDays - 1).operations: An array of Operation objects. Each Operation has a type field ("update" or "query").
type is "update":
startDay: number, the start index of the day range (inclusive).endDay: number, the end index of the day range (inclusive).percentageChange: number, the percentage change (e.g., 5 for +5%, -10 for -10%).type is "query":
startDay: number, the start index of the day range (inclusive).endDay: number, the end index of the day range (inclusive).number[], an array of results for each "query" operation, in the order they appear in operations.percentageChange can be positive or negative. A percentageChange of P means multiplying by (1 + P/100).Examples
Example 1
Input:
numDays = 5, operations = [ { type: "update", startDay: 0, endDay: 2, percentageChange: 10 }, { type: "query", startDay: 0, endDay: 4 }, { type: "update", startDay: 1, endDay: 3, percentageChange: -5 }, { type: "query", startDay: 0, endDay: 4 } ]
Output:
[1.331,1.139989375]
Note:
Initial multipliers: [1.0, 1.0, 1.0, 1.0, 1.0]
update(0, 2, 10): Multipliers for days 0, 1, 2 become *1.1. Array: [1.1, 1.1, 1.1, 1.0, 1.0]query(0, 4): Product 1.1 * 1.1 * 1.1 * 1.0 * 1.0 = 1.331update(1, 3, -5): Multipliers for days 1, 2, 3 become *0.95. Array: [1.1, 1.1*0.95, 1.1*0.95, 1.0*0.95, 1.0] => [1.1, 1.045, 1.045, 0.95, 1.0]query(0, 4): Product 1.1 * 1.045 * 1.045 * 0.95 * 1.0 = 1.139989375Example 2
Input:
numDays = 1, operations = [ { type: "update", startDay: 0, endDay: 0, percentageChange: 20 }, { type: "query", startDay: 0, endDay: 0 }, { type: "update", startDay: 0, endDay: 0, percentageChange: -10 }, { type: "query", startDay: 0, endDay: 0 } ]
Output:
[1.2,1.08]
Note:
Initial multipliers: [1.0]
update(0, 0, 20): Multiplier for day 0 becomes *1.2. Array: [1.2]query(0, 0): Product 1.2update(0, 0, -10): Multiplier for day 0 becomes *0.9. Array: [1.2 * 0.9 = 1.08]query(0, 0): Product 1.08Example 3
Input:
numDays = 3, operations = [ { type: "query", startDay: 0, endDay: 2 }, { type: "update", startDay: 0, endDay: 0, percentageChange: 50 }, { type: "update", startDay: 1, endDay: 1, percentageChange: -20 }, { type: "query", startDay: 0, endDay: 1 }, { type: "update", startDay: 2, endDay: 2, percentageChange: 100 }, { type: "query", startDay: 0, endDay: 2 } ]
Output:
[1.0,1.2,2.4]
Note:
Initial multipliers: [1.0, 1.0, 1.0]
query(0, 2): Product 1.0 * 1.0 * 1.0 = 1.0update(0, 0, 50): Day 0 becomes *1.5. Array: [1.5, 1.0, 1.0]update(1, 1, -20): Day 1 becomes *0.8. Array: [1.5, 0.8, 1.0]query(0, 1): Product 1.5 * 0.8 = 1.2update(2, 2, 100): Day 2 becomes *2.0. Array: [1.5, 0.8, 2.0]query(0, 2): Product 1.5 * 0.8 * 2.0 = 2.4Constraints
1 <= numDays <= 10^51 <= operations.length <= 10^50 <= startDay <= endDay < numDays-99 <= percentageChange <= 1000 (This ensures multipliers remain positive, 1 - 99/100 = 0.01 minimum)O(log N).O(N).Loading editor...