This is an easy DP problem, which can be solved using bottom-up DP.
Let us take an array a[] which keeps the number of coins ith monster keeps.
Lets make a new array dp[]
dp[0]=0;
and further for every index i
dp[i] = max(a[i] + dp[i-2], dp[i-1])
I hope you can implement the code yourself or refer to the following code:
Let us take an array a[] which keeps the number of coins ith monster keeps.
Lets make a new array dp[]
dp[0]=0;
and further for every index i
dp[i] = max(a[i] + dp[i-2], dp[i-1])
I hope you can implement the code yourself or refer to the following code:
0 comments:
Post a Comment