Initialize = 0 for all i. Then iterate by chain length from 2 to . For each length, iterate over all starting positions i. Compute j = i + length - . Try all split points and take the minimum. The 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: .