Flood Fill , Different states for storing in DP

Can, the recurrence for this question be :
dp[i][j] = for all x min(dp[i][x] + dp[x][j] + 2 ) if arr[i] not equal to arr[j] else
min(dp[i][x] + dp[x][j] + 1 ).The intution behind this approach is trying all possible conversions i.e. the ith element to all colors from (1 to i-1).

Hi @Vishal_234 ur solution seems right , but this solution is not feasible at all as in every state (i,j) u are traversing i to j making your complexity O(n^3) . So ,its better to stick our original approach i.e
for the case when arr[i]=arr[j] then dp[i][j] = 1+dp[i-1][j-1]
else dp[i][j]=1+min(dp[i+1][j],dp[i][j-1])

In case of any doubt feel free to ask :slight_smile:
If you got the answer mark ur doubt as resolved

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.