I'm trying to solve this topcoder problem. I have read the solution analysis but still cannot understand.
The basic idea of the solution is to think backwards and insert element instead of deleting. But how can this reduce the complexity of the problem ?
I do understand that this is a dynamic programming problem. I read on the wikipedia that DP problem avoid solving sub problems repeatly and thus reducing the complexity. But I do not see any sub problem redundency here.
Thanks
Problem Statement The Casket of Star (sic) is a device in the Touhou universe. Its purpose is to generate energy rapidly. Initially it contains n stars in a row. The stars are labeled 0 through n-1 from the left to the right. You are given a int[] weight, where weight[i] is the weight of star i.
The following operation can be repeatedly used to generate energy:
Choose a star x other than the very first star and the very last star.
The x-th star disappears.
This generates weight[x-1] * weight[x+1] units of energy.
We decrease n and relabel the stars 0 through n-1 from the left to the right.
Your task is to use the device to generate as many units of energy as possible. Return the largest possible total amount of generated energy.
n = 3
. Then think aboutn = 4
, remembering what you've already thought about. Then think aboutn = 5
, remembering what you've already thought about. Then think about all the things you've thought about so far.