|
| 1 | +# [Problem 3652: Best Time to Buy and Sell Stock using Strategy](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-using-strategy/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We are given prices and a strategy array where strategy[i] ∈ {-1,0,1} and profit = sum(strategy[i] * prices[i]). We may modify exactly one contiguous k-length subarray by setting its first k/2 entries to 0 and last k/2 entries to 1. We want the maximum profit after at most one such modification. |
| 5 | + |
| 6 | +Brute force would try every possible window and compute the new profit by replacing values — that's O(n*k) which is too slow (n up to 1e5). Need an O(n) or O(n log n) approach. |
| 7 | + |
| 8 | +For a window [l, l+k-1], the profit change (delta) equals sum over first half (new - old) + sum over second half (new - old). For first half new is 0 so contribution is -strategy[i]*price[i]. For second half new is 1 so contribution is (1 - strategy[i])*price[i]. So delta = sum_first(-s*p) + sum_second((1-s)*p). That looks like two separate sums per window, but there may be a simplification. |
| 9 | + |
| 10 | +## Refining the problem, round 2 thoughts |
| 11 | +Compute arrays: |
| 12 | +- A[i] = -strategy[i] * prices[i] (contribution when i falls in the first half of the modified window) |
| 13 | +- B[i] = (1 - strategy[i]) * prices[i] (contribution when i falls in the second half) |
| 14 | + |
| 15 | +Observe B[i] - A[i] = (1 - s)p - (-s p) = p, i.e., B = A + p. So for a window: |
| 16 | +delta = sum_{i=l}^{l+k-1} A[i] + sum_{i=l+m}^{l+k-1} p[i], where m = k/2. |
| 17 | +Thus delta = sumA(window) + sumP(second_half_of_window). |
| 18 | + |
| 19 | +We can compute prefix sums for A and for prices p. Then for each l we compute: |
| 20 | +S_A = prefA[l+k] - prefA[l] |
| 21 | +S_P_second = prefP[l+k] - prefP[l+m] |
| 22 | +delta = S_A + S_P_second |
| 23 | + |
| 24 | +Compute baseProfit = sum(strategy[i]*prices[i]). The best profit is baseProfit + max(0, max_delta_over_windows). Complexity O(n) time and O(n) extra for prefix arrays (or O(1) extra if we compute sliding sums on the fly). |
| 25 | + |
| 26 | +Edge cases: |
| 27 | +- No modification may be beneficial (we take max with 0). |
| 28 | +- k can be equal to n; sliding still works. |
| 29 | +- All integers fit in Python ints. |
| 30 | + |
| 31 | +## Attempted solution(s) |
| 32 | +```python |
| 33 | +from typing import List |
| 34 | + |
| 35 | +class Solution: |
| 36 | + def maxProfit(self, prices: List[int], strategy: List[int], k: int) -> int: |
| 37 | + n = len(prices) |
| 38 | + m = k // 2 |
| 39 | + |
| 40 | + # base profit without any modification |
| 41 | + base_profit = 0 |
| 42 | + for s, p in zip(strategy, prices): |
| 43 | + base_profit += s * p |
| 44 | + |
| 45 | + # prefix sums for A[i] = -strategy[i] * prices[i] and for prices |
| 46 | + prefA = [0] * (n + 1) |
| 47 | + prefP = [0] * (n + 1) |
| 48 | + for i in range(n): |
| 49 | + prefA[i+1] = prefA[i] + (-strategy[i]) * prices[i] |
| 50 | + prefP[i+1] = prefP[i] + prices[i] |
| 51 | + |
| 52 | + max_delta = 0 # we can choose not to modify, so delta >= 0 |
| 53 | + |
| 54 | + # iterate over all windows of length k |
| 55 | + for l in range(0, n - k + 1): |
| 56 | + r = l + k |
| 57 | + sumA = prefA[r] - prefA[l] |
| 58 | + sumP_second = prefP[r] - prefP[l + m] |
| 59 | + delta = sumA + sumP_second |
| 60 | + if delta > max_delta: |
| 61 | + max_delta = delta |
| 62 | + |
| 63 | + return base_profit + max_delta |
| 64 | +``` |
| 65 | +- Notes: |
| 66 | + - We derived a simplification B = A + p which led to delta = sumA(window) + sumP(second_half). This allows O(1) computation per window using prefix sums. |
| 67 | + - Time complexity: O(n) to build prefix sums and O(n) to sweep all windows => O(n) overall. |
| 68 | + - Space complexity: O(n) for two prefix arrays (can be reduced to O(1) by maintaining running window sums, but O(n) is simple and well within limits for n ≤ 1e5). |
| 69 | + - We return base_profit + max(0, best_delta) — in the code max_delta initialized to 0 ensures we never decrease profit by applying the modification. |
0 commit comments