FastPrepFastPrep
Problem Brief

Maximize Profit

OA
See Tiktok online assessment and hiring insights

You are given an array called Rates, where rates[i] represents the currency price on the ith day.

You are also given an array called Strategy, where strategy[i] represents an operation. Each operation could be -1 for buy, 0 for hold, and 1 for sell.

You are also given k, which is guaranteed to be an even integer.

You can change the Strategy array like so:

  • Choose a range of k consecutive elements.
  • Set the first half of this range to 0.
  • Set the second half to 1.
  • Choose an optimal range to change the Strategy array so as to maximize the profit from executing the strategy. Return this maximum profit.

    The profit is defined as the sum of all selling rates minus the sum of all buying rates. For example, if rates = [2, 3, 4, 5] and strategy = [1, 0, 0, -1], then your total profit would be (2 * 1) + (5 * -1) = -3.

    1Example 1

    Input
    strategy = [2, 3, 4, 5], rates = [1, 0, 0, -1], k = 4
    Output
    -3
    Explanation
    Note - k value in this example is temperorily missing. I will add it once find reliable source :) Your total profit would be (2 * 1) + (5 * -1) = -3

    Constraints

    Limits and guarantees your solution can rely on.

    🥹
    public int maxProfit(int[] strategy, int[] rates, int k) {
      // write your code here
    }
    
    Input

    strategy

    [2, 3, 4, 5]

    rates

    [1, 0, 0, -1]

    k

    4

    Output

    -3

    Sign in to submit your solution.