Earn the Most π
Problem statement
You are given two equal-length arrays, prices and approach. For each day i, prices[i] is the currency price and approach[i] describes the trading action:
-1means buy and contributes-prices[i]to profit.0means hold and contributes0.1means sell and contributesprices[i].
You must choose exactly one contiguous window of length mrK. Rewrite the first half of that window to hold actions and the second half to sell actions. Actions outside the chosen window remain unchanged.
Return the maximum total profit obtainable after this rewrite, where total profit is the sum of every day's resulting contribution.
Function
earnTheMost(prices: int[], approach: int[], mrK: int) β intExamples
Example 1
prices = [2, 4, 1, 5, 10, 6]approach = [-1, 1, 0, 1, -1, 0]mrK = 4return = 18The original strategy contributes -2 + 4 + 0 + 5 - 10 + 0 = -3.
Choose the window covering indices 2 through 5. Its rewritten actions are [0, 0, 1, 1], so the complete strategy becomes [-1, 1, 0, 0, 1, 1].
The resulting profit is -2 + 4 + 0 + 0 + 10 + 6 = 18, which is the maximum over all legal windows.
Constraints
prices.length = approach.length2 ≤ mrK ≤ prices.lengthmrKis even.- Every
prices[i]is a positive integer. - Every
approach[i]is-1,0, or1. - The answer fits in a signed 32-bit integer.