I am finding a hard time solving Sherlock and Cost. it is given in dynamic programming section but i am not able to think it recursively or in a dp way ..... here's the link for the problem...
https://www.hackerrank.com/challenges/sherlock-and-cost/problem
can anyone tell how to approach the problem .. i know the fact that only 1 or the b[i] would be in the answer.!!!
You already know the states. Each cell can have two values so, let dp[i][0] be the max sum you can form if the ith no. is equal to 1 and dp[i][1] be the max sum you can form if the ith no. is equal to b[i].
okay i got it. but how do we know that it is optimal? i'm a newbie and having a bit trouble solving it... if u could please tell me
For the ith number, there are 2 cases:
Take max of both cases, it will give you the optimal answer for a[i] = 1.
PS: If you're unable to get it from my comment, I'd strictly recommend you to first learn dp and then attempt these problems.