peti29 wrote:Solomon: thank you for the detailed explanation! I almost get it now. Only one thing remains:How is it guaranteed that we'll use a piece only once?
Say our test case is: '(', '(', ')', ')' then our positive memo table will be [0, 1, 2] and our negative memo table the same,
so when we compare memo table entries we will add 4 for i==2 which is correct, but then we'll add 2 for i==1 resulting in a sum of 6 which is incorrect.
Once we've calculated the memo tables, the dynamic programming step is done! As we're stepping through the memo tables and find cases where there are positive values in the same index across the tables, we're just trying to find the max (and we're going through them in regular order). So at i=1, we see values 1 and 1 across the tables, add them, and set that as our current max. Then at i=2, we see values 2 and 2 across the tables, add them, see that this value is higher, and set that as our new max, which is our answer.