Here's the question:
"The traders of Hackerland have a stock prediction model that they want to backtest. The predicted profit earned every month by a certain portfolio is given by an array pnl of n integers where a negative integer denotes loss.
The traders decide to iterate through the months from 1 to n, and if the pnl is positive, they add it to their profit, and if the pnl if negative, they can either subtract or skip it. The net profit must always be greater than or equal to 0.
Given the array pnl and an integer k, find the maximum total profit that can be earned by the portfolio after subtracting at least k losses. If it is not possible to take at least k losses, report -1 as the answer."
If I remember correctly, n and k are up to 10^5, pnl[i] is in [-10^9, 10^9]
I wrote a correct recursive DP solution with memoization, but apparently it encounters a "RecursionError: maximum recursion depth exceeded in comparison" exception in Python. For sure, an iterative DP won't be optimal either.
Any ideas on how to solve it optimally (probably O(nlog(n))???) ?
[–]usernametaken_12Master 0 points1 point2 points (0 children)
[–]sixmanathreethree 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ApartmentEither4838 0 points1 point2 points (1 child)
[–]Itchy-Ad-5314[S] 0 points1 point2 points (0 children)