We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Sherlock and Cost
Sherlock and Cost
+ 0 comments Bottom up DP
int cost(vector<int> B) { vector<vector<int>>dp(B.size(),vector<int>(2,0)); int n=dp.size()-1; for(int i=1;i<B.size();i++){ dp[i][0]=max(dp[i-1][0],dp[i-1][1]+abs(B[i-1]-1)); dp[i][1]=max(dp[i-1][0]+abs(B[i]-1),dp[i-1][1]+abs(B[i]-B[i-1])); } return max(dp[n][0],dp[n][1]); }
+ 0 comments Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Sherlock and Cost Problem Solution
+ 2 comments I see this as a convex maximization problem over a closed bounded set which means the solution is on the boundary (e.g., think of maximizing
abs(x) for a <= x <= b
. This meansA[i] = 1 or B[i]
. The rest is simple DP.def cost(B): # Write your code here Smax = 0 Smin = 0 for index, value in enumerate(B): if index == 0: continue smin = max([Smin, Smax + abs(B[index - 1] - 1)]) Smax = max([Smin + abs(1 - B[index]), Smax + abs(B[index] - B[index - 1])]) Smin = smin return max([Smin, Smax])
+ 0 comments Here is the solution of Sherlock and Cost Click Here
+ 0 comments Here is problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-sherlock-and-cost-problem-solution.html
Load more conversations
Sort 249 Discussions, By:
Please Login in order to post a comment