|
|
back to boardyet one more dp solution explanation as other participants i used dp. there are different explanations of dp approach, but somehow i find them pretty blurry. when calculating the min number of brackets to be added for the substring, i think there should be done only 2 simple steps: 1) check whether the substring is already correct - can be done using stack in linear time. if this happens to be true, than no sense going to step 2 2) if s is substring and d[i][j] is used for storing dynamic for substring of i length starting at j, then solution is: d[i][j] = min(d[i - 2][j + 1] if s[j] == s[i + j], min(d[k][j] + d[i - k][j + k]) for 1 <= k < i)) why the 2nd step has that condition? that goes from the the problem definition itself. we can form regular sequence either from wrapping it in brackets, eg '( (....) )' or with concatenating two regular sequences, eg '( ...) [ ... ]'. hope this explanation brings some clarity on the approach. |
|
|