Pad the array with 1s: new_nums = [] + nums + []. Now work with indices to n+. Iterate by gap size from to . For each gap, iterate starting positions. Try all in between. Answer is . Time: , Space: . The implementation follows directly from the dp formula. Each line of code corresponds to part of the mathematical formula. Walk through a small example step by step to verify your understanding.
Time complexity: .
Space complexity: .