leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 2b8a977be2fc02f7a5050d3301d9b79015c98a0b
parent ee529c9e77c3d65b74086a2afb111b1fcf030ff2
Author: Jack Sangdahl <jack@pngu.org>
Date:   Wed, 17 Dec 2025 22:24:04 -0700

3652. best time to buy and sell stock using strategy

Diffstat:
Abest_time_buy_sell_stock_strat.c | 41+++++++++++++++++++++++++++++++++++++++++
Abest_time_buy_sell_stock_strat.py | 43+++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/best_time_buy_sell_stock_strat.c b/best_time_buy_sell_stock_strat.c @@ -0,0 +1,41 @@ +// 3652. Best Time to Buy and Sell Stock using Strategy +#include "leetcode.h" + +/* + * you are given two integer arrays 'prices' and 'strategy' where 'prices[i]' is + * the price of a given stock, and 'strategy[i]' represents a trading action + * taken: -1 indicates buying one unit of the stock, 0 indicates holding the + * stock, 1 indicates selling one unit of the stock. you are also given an even + * integer 'k' and may perform at most one modification to 'strategy'. the + * profit is defined as the sum of 'strategy[i] * prices[i]' across all days. + * return the maximum possible profit you can achieve. + */ + +long long maxProfit(int *prices, int pricesSize, int *strategy, + int strategySize, int k) { + long long ans = 0, new = 0; + for (int i = 0; i < pricesSize; ++i) + ans += prices[i] * strategy[i]; + for (int i = k / 2; i < k; ++i) + new += prices[i]; + for (int i = k; i < pricesSize; ++i) + new += prices[i] * strategy[i]; + ans = fmax(ans, new); + for (int i = 0; i < pricesSize - k; ++i) { + int base = i + k; + new += prices[base - k] * strategy[base - k]; + new -= prices[base - (k / 2)]; + new += prices[base] * (1 - strategy[base]); + ans = fmax(ans, new); + } + return ans; +} + +int main() { + int p1[] = {4, 2, 8}, s1[] = {-1, 0, 1}; + int p2[] = {5, 4, 3}, s2[] = {1, 1, 0}; + printf("%lld\n", + maxProfit(p1, ARRAY_SIZE(p1), s1, ARRAY_SIZE(s1), 2)); // expect: 10 + printf("%lld\n", + maxProfit(p2, ARRAY_SIZE(p2), s2, ARRAY_SIZE(s2), 2)); // expect: 9 +} diff --git a/best_time_buy_sell_stock_strat.py b/best_time_buy_sell_stock_strat.py @@ -0,0 +1,43 @@ +# 3652. 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, and 'strategy[i]' represents a trading action +taken: -1 indicates buying one unit of the stock, 0 indicates holding the +stock, 1 indicates selling one unit of the stock. you are also given an even +integer 'k' and may perform at most one modification to 'strategy'. the +profit is defined as the sum of 'strategy[i] prices[i]' across all days. +return the maximum possible profit you can achieve. +""" + + +class Solution(object): + def maxProfit(self, prices, strategy, k): + """ + :type prices: List[int] + :type strategy: List[int] + :type k: int + :rtype: int + """ + n, ans = len(prices), 0 + for i in range(n): + ans += prices[i] * strategy[i] + new = ans + for i in range(n): + if i > k - 1: + new += prices[i - k] * strategy[i - k] + new -= prices[i - k // 2] + if i < k // 2: + new += prices[i] * (-1 * strategy[i]) + else: + new += prices[i] * (-1 * strategy[i]) + new += prices[i] + if i >= k - 1: + ans = max(ans, new) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxProfit(prices=[4, 2, 8], strategy=[-1, 0, 1], k=2)) + print(obj.maxProfit(prices=[5, 4, 3], strategy=[1, 1, 0], k=2))