3652. Best Time to Buy and Sell Stock using Strategy
Difficulty: Medium
Topics: Array, Sliding Window, Prefix Sum, Weekly Contest 463
You are given two integer arrays prices and strategy, where:
-
prices[i]is the price of a given stock on theiᵗʰday. -
strategy[i]represents a trading action on theiᵗʰday, 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 = 2
- Output: 10
- Explanation:
| Modification | Strategy | Profit Calculation | Profit |
|---|---|---|---|
| Original | [-1, 0, 1] | (-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8 | 4 |
| Modify [0, 1] | [0, 1, 1] | (0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8 | 10 |
| Modify [1, 2] | [-1, 0, 1] | (-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8 | 4 |
Thus, the maximum possible profit is 10, which is achieved by modifying the subarray [0, 1].
Example 2:
- Input: prices = [5,4,3], strategy = [1,1,0], k = 2
- Output: 9
- Explanation:
| Modification | Strategy | Profit Calculation | Profit |
|---|---|---|---|
| Original | [1, 1, 0] | (1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0 | 9 |
| Modify [0, 1] | [0, 1, 0] | (0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0 | 4 |
| Modify [1, 2] | [1, 0, 1] | (1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3 | 8 |
Thus, the maximum possible profit is 9, which is achieved without any modification.
Constraints:
2 <= prices.length == strategy.length <= 10⁵1 <= prices[i] <= 10⁵-1 <= strategy[i] <= 12 <= k <= prices.length-
kis even
Hint:
- Use prefix sums to precompute the base profit and to get fast range queries (sums of
pricesand counts of eachstrategyvalue over any interval). - Try every segment of length
k: compute the profit delta caused by replacing that segment (using the prefix queries) and take the maximum ofbase + delta.
Solution:
We can solve this problem efficiently using prefix sums to compute the profit change for any modification in O(1) time, after O(n) preprocessing.
Approach:
- Compute Base Profit: Start by calculating the original profit without any modifications.
- Prefix Sums for Efficiency: Precompute prefix sums to quickly calculate sums over any subarray in O(1) time.
- Analyze Modification Impact: For each possible modification window, compute the profit change compared to the original.
- Optimize with Sliding Window: Consider all possible windows of length k and track the maximum improvement.
- Combine Results: Add the maximum possible improvement to the base profit.
Let's implement this solution in PHP: 3652. Best Time to Buy and Sell Stock using Strategy
<?php
/**
* @param Integer[] $prices
* @param Integer[] $strategy
* @param Integer $k
* @return Integer
*/
function maxProfit($prices, $strategy, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo maxProfit([4,2,8], [-1,0,1], 2) . "\n"; // Output: 10
echo maxProfit([5,4,3], [1,1,0], 2) . "\n"; // Output: 9
?>
Explanation:
-
Base Profit Calculation:
- Calculate the initial profit as
sum(strategy[i] * prices[i])for all days. - This represents the profit without any modifications.
- Calculate the initial profit as
-
Prefix Sum Arrays:
- Create
prefixPrices: cumulative sum ofprices, allows O(1) range sum queries. - Create
prefixSp: cumulative sum ofstrategy[i] * prices[i], allows O(1) queries for original profit in any window.
- Create
-
Understanding the Modification:
- When modifying a window of length k:
- First k/2 elements become 0 (hold) → their contribution becomes 0.
- Last k/2 elements become 1 (sell) → their contribution becomes
prices[i].
- Original profit in window:
sum(strategy[i] * prices[i])for i in window. - New profit in window:
sum(prices[i])for i in second half of window. - Change in profit (delta) =
new_profit - old_profit.
- When modifying a window of length k:
-
Efficient Delta Calculation:
- For window [l, l+k-1]:
-
sumPricesSecondHalf= sum of prices from l+half to l+k-1. -
sumOldSegment= original profit contribution from entire window. -
delta = sumPricesSecondHalf - sumOldSegment.
-
- For window [l, l+k-1]:
-
Iterate Through All Windows:
- Try every possible starting position l where window fits (0 ≤ l ≤ n-k).
- Track maximum delta found.
- If no positive delta exists, no modification is beneficial.
-
Final Result:
- Maximum profit = baseProfit + max(0, maxDelta).
Complexity Analysis
- Time Complexity: O(n) - Single pass to build prefix arrays, then O(n) to check all windows.
- Space Complexity: O(n) - For storing prefix sums.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)