I am fairly new to dp and practicing to get better. Can anyone explain this problem from codeforces educational round. There is an editorial but still I am not convinced with the intution. Can anyone help me explain it? asked 31 Dec '18, 00:23

Here we can memoize our solution in the DP array, let dp[i][4] be the cost of making the string easy the index till 1<=i<=N where ith character can be any character of "hard". If the ith character of our string is 'h' then dp[i][0] = dp[i1][0] + cost[i]. If the ith character of our string is 'a' then dp[i][1] = we have 2 options whether to take dp[i1][0] i.e. cost of making string easy removing only character 'h' or second option is to add the current cost of 'a' to the previously calculated cost of 'a' i.e. dp[i1][1] + cost[i]. Similarly, If the ith character of our string is 'r' then dp[i][2] = we have 2 options whether to take dp[i1][1] i.e. cost of making string easy removing only character 'a' or second option is to add the cost of 'r' to the previously calculated cost of 'r' i.e. dp[i1][2] + cost[i]. Similarly for 'd' we have dp[i][3] = minimum of dp[i1][2] // Taking only cost till first i1 index and having character 'r' or take dp[i1][3] i.e. adding 'd' cost into in the previous calculated cost of 'd'.
answered 10 Feb, 13:28
