Minimize the cost of an array where
cost = summation of square of difference of two consecutive array elements.
ex:
arr = [1,2,3,4]
sum = (1-2)^2 + (2-3)^2 + (3-4)^2
Output = 3
entered_list = input("Enter a list, separating numbers with spaces: ").split()
test_list = list(map(int, entered_list))
cost = [((a - b) ** 2) for a, b in zip(test_list, test_list[1:])]
print("Cost: ", sum(cost))
Comments
Leave a comment