FastPrepEarn the Most 🐝

Earn the Most 🐝

Capital One logoCapital One● MediumFULLTIMEOA
Learn

Problem statement

You are given two equal-length arrays, prices and approach. For each day i, prices[i] is the currency price and approach[i] describes the trading action:

  • -1 means buy and contributes -prices[i] to profit.
  • 0 means hold and contributes 0.
  • 1 means sell and contributes prices[i].

You must choose exactly one contiguous window of length mrK. Rewrite the first half of that window to hold actions and the second half to sell actions. Actions outside the chosen window remain unchanged.

Return the maximum total profit obtainable after this rewrite, where total profit is the sum of every day's resulting contribution.

Function

earnTheMost(prices: int[], approach: int[], mrK: int) β†’ int

Examples

Example 1

prices = [2, 4, 1, 5, 10, 6]approach = [-1, 1, 0, 1, -1, 0]mrK = 4return = 18

The original strategy contributes -2 + 4 + 0 + 5 - 10 + 0 = -3.

Choose the window covering indices 2 through 5. Its rewritten actions are [0, 0, 1, 1], so the complete strategy becomes [-1, 1, 0, 0, 1, 1].

The resulting profit is -2 + 4 + 0 + 0 + 10 + 6 = 18, which is the maximum over all legal windows.

Constraints

  • prices.length = approach.length
  • 2 ≤ mrK ≤ prices.length
  • mrK is even.
  • Every prices[i] is a positive integer.
  • Every approach[i] is -1, 0, or 1.
  • The answer fits in a signed 32-bit integer.

More Capital One problems

See Capital One hiring insights
public int earnTheMost(int[] prices, int[] approach, int mrK) {
    // write your code here
}
prices[2, 4, 1, 5, 10, 6]
approach[-1, 1, 0, 1, -1, 0]
mrK4
expected18
Checking account…