Problem · Array
Maximize Profit
Learn this problemProblem statement
You are given arrays rates and strategy. The value rates[i] is the currency price on day i, while strategy[i] is:
-1to buy,0to hold, or1to sell.
The profit of a strategy is sum(strategy[i] * rates[i]).
You must modify exactly one range of k consecutive positions, where k is even:
- Set the first half of the chosen range to
0. - Set the second half of the chosen range to
1.
Choose the range that maximizes the final profit and return that maximum profit.
Function
maxProfit(strategy: int[], rates: int[], k: int) → intExamples
Example 1
strategy = [1, 0, 0, -1]rates = [2, 3, 4, 5]k = 4return = 9The only length-4 range is the whole array. It changes the strategy to [0, 0, 1, 1], giving profit 4 + 5 = 9.
Constraints
strategy.length = rates.length- Every
strategy[i]is-1,0, or1. - Every
rates[i]is a non-negative 32-bit integer. 2 <= k <= strategy.lengthkis even.- The absolute value of every possible final profit fits in a signed 32-bit integer.
More Tiktok problems
- Count Access Code PairsOA · Seen Jul 2026
- Count Key ChangesOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Count Skipped Numbers After SubtractionsOA · Seen Jul 2026
- Obstacle Placement QueriesOA · Seen Jul 2026
- Repeated Grouped Digit SumOA · Seen Jul 2026
- Count Cyclic Digit PairsOA · Seen Jun 2026
- Event ID Check Completion TimesOA · Seen Jun 2026