Problem Statement:

https://www.hackerrank.com/challenges/sherlock-and-cost/problem

Solution:

def cost(arr):
    a=b=0
    for i in range(1,len(arr)):
        new_a=max((abs(arr[i]-arr[i-1])+a),(abs(arr[i]-1)+b))
        new_b=max((abs(1-arr[i-1])+a),b)
        a=new_a
        b=new_b
    return max(a,b)    

Explanation:

Note:
You can also join our membership for clearning your doubts or for any personal learning for Python , C , Data Structure using C or Python. Link is below:

https://www.youtube.com/channel/UCj98X_6ZxT9Xcx7NO0ECXZw/join

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top