Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

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

    Medium

    You are given two integer arrays prices and strategy, where:

    • prices[i] is the price of a given stock on the <code>i<sup>th</sup></code> day.

    • strategy[i] represents a trading action on the <code>i<sup>th</sup></code> day, where:

    You are also given an even integer k, and may perform at most one modification to strategy. A modification consists of:

    • Selecting exactly k consecutive elements in strategy.

    • Set the first k / 2 elements to 0 (hold).

    • Set the last k / 2 elements to 1 (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:

    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:

    Thus, the maximum possible profit is 9, which is achieved without any modification.

    Constraints:

    • <code>2 <= prices.length == strategy.length <= 10<sup>5</sup></code>

    • <code>1 <= pricesi<= 10<sup>5</sup></code>

    • -1 &lt;= strategy[i] &lt;= 1

    • 2 &lt;= k &lt;= prices.length

    • k is even

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Long maxProfit(IntArray p, IntArray s, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait