3652. Best Time to Buy and Sell Stock using Strategy Medium

@problem@discussion
#Array#Sliding Window#Prefix Sum



1/**
2 * [3652] Best Time to Buy and Sell Stock using Strategy
3 *
4 * You are given two integer arrays prices and strategy, where:
5 * 
6 * 	prices[i] is the price of a given stock on the i^th day.
7 * 	strategy[i] represents a trading action on the i^th day, where:
8 * 	
9 * 		-1 indicates buying one unit of the stock.
10 * 		0 indicates holding the stock.
11 * 		1 indicates selling one unit of the stock.
12 * 	
13 * 	
14 * 
15 * You are also given an even integer k, and may perform at most one modification to strategy. A modification consists of:
16 * 
17 * 	Selecting exactly k consecutive elements in strategy.
18 * 	Set the first k / 2 elements to 0 (hold).
19 * 	Set the last k / 2 elements to 1 (sell).
20 * 
21 * The profit is defined as the sum of strategy[i] * prices[i] across all days.
22 * Return the maximum possible profit you can achieve.
23 * Note: There are no constraints on budget or stock ownership, so all buy and sell operations are feasible regardless of past actions.
24 *  
25 * <strong class="example">Example 1:
26 * <div class="example-block">
27 * Input: <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span>
28 * Output: <span class="example-io">10</span>
29 * Explanation:
30 * <table style="border: 1px solid black;">
31 * 	<thead>
32 * 		<tr>
33 * 			<th style="border: 1px solid black;">Modification</th>
34 * 			<th style="border: 1px solid black;">Strategy</th>
35 * 			<th style="border: 1px solid black;">Profit Calculation</th>
36 * 			<th style="border: 1px solid black;">Profit</th>
37 * 		</tr>
38 * 	</thead>
39 * 	<tbody>
40 * 		<tr>
41 * 			<td style="border: 1px solid black;">Original</td>
42 * 			<td style="border: 1px solid black;">[-1, 0, 1]</td>
43 * 			<td style="border: 1px solid black;">(-1 &times; 4) + (0 &times; 2) + (1 &times; 8) = -4 + 0 + 8</td>
44 * 			<td style="border: 1px solid black;">4</td>
45 * 		</tr>
46 * 		<tr>
47 * 			<td style="border: 1px solid black;">Modify [0, 1]</td>
48 * 			<td style="border: 1px solid black;">[0, 1, 1]</td>
49 * 			<td style="border: 1px solid black;">(0 &times; 4) + (1 &times; 2) + (1 &times; 8) = 0 + 2 + 8</td>
50 * 			<td style="border: 1px solid black;">10</td>
51 * 		</tr>
52 * 		<tr>
53 * 			<td style="border: 1px solid black;">Modify [1, 2]</td>
54 * 			<td style="border: 1px solid black;">[-1, 0, 1]</td>
55 * 			<td style="border: 1px solid black;">(-1 &times; 4) + (0 &times; 2) + (1 &times; 8) = -4 + 0 + 8</td>
56 * 			<td style="border: 1px solid black;">4</td>
57 * 		</tr>
58 * 	</tbody>
59 * </table>
60 * Thus, the maximum possible profit is 10, which is achieved by modifying the subarray [0, 1]​​​​​​​.
61 * </div>
62 * <strong class="example">Example 2:
63 * <div class="example-block">
64 * Input: <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span>
65 * Output: <span class="example-io">9</span>
66 * Explanation:
67 * <div class="example-block">
68 * <table style="border: 1px solid black;">
69 * 	<thead>
70 * 		<tr>
71 * 			<th style="border: 1px solid black;">Modification</th>
72 * 			<th style="border: 1px solid black;">Strategy</th>
73 * 			<th style="border: 1px solid black;">Profit Calculation</th>
74 * 			<th style="border: 1px solid black;">Profit</th>
75 * 		</tr>
76 * 	</thead>
77 * 	<tbody>
78 * 		<tr>
79 * 			<td style="border: 1px solid black;">Original</td>
80 * 			<td style="border: 1px solid black;">[1, 1, 0]</td>
81 * 			<td style="border: 1px solid black;">(1 &times; 5) + (1 &times; 4) + (0 &times; 3) = 5 + 4 + 0</td>
82 * 			<td style="border: 1px solid black;">9</td>
83 * 		</tr>
84 * 		<tr>
85 * 			<td style="border: 1px solid black;">Modify [0, 1]</td>
86 * 			<td style="border: 1px solid black;">[0, 1, 0]</td>
87 * 			<td style="border: 1px solid black;">(0 &times; 5) + (1 &times; 4) + (0 &times; 3) = 0 + 4 + 0</td>
88 * 			<td style="border: 1px solid black;">4</td>
89 * 		</tr>
90 * 		<tr>
91 * 			<td style="border: 1px solid black;">Modify [1, 2]</td>
92 * 			<td style="border: 1px solid black;">[1, 0, 1]</td>
93 * 			<td style="border: 1px solid black;">(1 &times; 5) + (0 &times; 4) + (1 &times; 3) = 5 + 0 + 3</td>
94 * 			<td style="border: 1px solid black;">8</td>
95 * 		</tr>
96 * 	</tbody>
97 * </table>
98 * Thus, the maximum possible profit is 9, which is achieved without any modification.
99 * </div>
100 * </div>
101 *  
102 * Constraints:
103 * 
104 * 	2 <= prices.length == strategy.length <= 10^5
105 * 	1 <= prices[i] <= 10^5
106 * 	-1 <= strategy[i] <= 1
107 * 	2 <= k <= prices.length
108 * 	k is even
109 * 
110 */
111pub struct Solution {}
112
113// problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-using-strategy/
114// discuss: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-using-strategy/discuss/?currentPage=1&orderBy=most_votes&query=
115
116// submission codes start here
117
118impl Solution {
119    pub fn max_profit(prices: Vec<i32>, strategy: Vec<i32>, k: i32) -> i64 {
120        
121    }
122}
123
124// submission codes end
125
126#[cfg(test)]
127mod tests {
128    use super::*;
129
130    #[test]
131    fn test_3652() {
132    }
133}
134

Back
© 2026 bowen.ge All Rights Reserved.