Problem · Array
Profit Analysis
Learn this problemProblem statement
The profit and loss for each month is represented by an integer array pnl. A positive value represents profit earned in that month, while a negative value represents a loss.
Return the maximum net profit obtainable from any non-empty contiguous segment of months whose length is at most k.
Function
getMaxProfit(pnl: int[], k: int) → longExamples
Example 1
pnl = [-3,4,3,-2,2,5]k = 4return = 8The segment [3,-2,2,5] has length 4 and total profit 3 + (-2) + 2 + 5 = 8. The segment [4,3,-2,2,5] has total profit 12, but its length is 5, which exceeds k = 4.
Constraints
1 <= pnl.length <= 2 * 10^5-10^9 <= pnl[i] <= 10^91 <= k <= pnl.length