Best Time to Buy and Sell Stock using Strategy
You are given two integer arrays prices and strategy, where:
prices[i]is the price of a given stock on thei^thday.strategy[i]represents a trading action on thei^thday, where:-1indicates buying one unit of the stock.0indicates holding the stock.1indicates selling one unit of the stock.
You are also given an even integer k, and may perform at most one modification to strategy. A modification consists of:
- Selecting exactly
kconsecutive elements instrategy. - Set the first
k / 2elements to0(hold). - Set the last
k / 2elements to1(sell).
The profit is defined as the sum of strategy[i] * prices[i] across all days.
Return the maximum possible profit you can achieve.
Note: There are no constraints on budget or stock ownership, so all buy and sell operations are feasible regardless of past actions.
Example 1
Input
prices = [4,2,8], strategy = [-1,0,1], k = 2Output
10The maximum possible profit is 10, achieved by modifying the subarray [0, 1] so the strategy becomes [0, 1, 1].
Example 2
Input
prices = [5,4,3], strategy = [1,1,0], k = 2Output
9The maximum possible profit is 9, achieved without any modification.
Constraints
- 2 <= prices.length == strategy.length <= 10^5
- 1 <= prices[i] <= 10^5
- -1 <= strategy[i] <= 1
- 2 <= k <= prices.length
- k is even