I'm stuck in this problem 225C - Barcode
Here is my solution 33821210
I tried to calculate the array dp[current_column][width_of_current_group][hash_or_dot], and after i read the tutorial saying that i should calculate only dp[current_column][hash_or_dot] i didn't figure out why my solution was wrong.
I think they are the same, can't see any difference... can somebody help?
Auto comment: topic has been updated by WinstonWolf (previous revision, new revision, compare).
Both solutions have the same idea .. but yours uses more memory in vain. So either there's a bug in your code or you have a wrong dp state.
We have dp[pos][type][how_much] means that you're in the [pos] column and now you're trying to fill it with [type] (white or black) and you have [how_much] of them from previous columns, and it returns the minimum possible of pixels to repaint till the end.
dp[pos][type][how_much] = dp[pos + 1][type][how_much + 1] + arr[pos][type ^ 1]
And if (x <= how_much <= y) dp[pos][type][how_much] = min(dp[pos][type][how_much], dp[pos + 1][type ^ 1][1]+arr[pos][type]);
Where arr[pos][type] refers to number of white or black pixles in the column [pos].
So the answer is min(dp[0][1][0], dp[0][0][0]).
Thanks for your help :)
It was one silly fault in my implementation :"D
The following submission using C++ STL arrays and based on the problem tutorial may help you to improve the readability and efficiency of your solution.
33832273
Please note that computing the number of black and white pixels between columns
1
andj
inclusive is done prior to the DP main loop. The result is then used to compute the number of black and white pixels between columnsj
andi
inclusive in O(1) time using a simple subtraction operation.Best wishes