TikTok | OA | Maximise profit from strategy
Anonymous User
1099

Hello all, I would like to ask for help regarding an OA question I did recently. I think it's a sliding window problem, but I couldn't get by all test cases.

Question
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:

  1. choose a range of k consecutive elements
  2. set the first half of this range to 0
  3. set the second half to 1

Choose an optimal range to change the Strategy array so as to maximise 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

Inputs
strategy: list[int]
rates: list[int]
k: int #guaranteed to be an even number

output
maxProfit: int

Sorry, I couldn't remember what examples were given and the max input sizes, but it was such that you would TLE if you do a brute force solution. Also, if you know of a similar problem available online, it'd be great if you could link it!

Thanks in advance!

Comments (4)