This problem can be solved using dynamic programming.

Our DP will have two parameters—position and number of operation left to be used. So we can have a total of N×K states.

In each state, we will select a segment of the array and call the next state. As we can use maximum K operations, our inner loop will not run more than that.

Thus, our time complexity will be O(N × K2).

Statistics

48% Solution Ratio
tmwilliamlin168Earliest, Feb '20
Shubham.436400Fastest, 0.0s
Andres_UntLightest, 10 MB
Kuddus.6068Shortest, 492B
Toph uses cookies. By continuing you agree to our Cookie Policy.