Given an array of 1 and 0. We need to move all 1's to 0 with minimum cost
We can move either left or right , multiple one's cannot be moved to same position.
Each movement cost is 1.
Ex:
array = 0001101
Here the optimal solution is 5 , x means it cannot be occupied
1. 3rd index to 2nd index -> cost = 1 , array = 00xx101
2. 4th index to 1st index -> cost = 3 , array = 0xxxx01
3. 6th index to 5th index -> cost = 1 , array = 0xxxxxx
I tried it by bruteforce way of finding it's nearest 0 and moving it , but with no success. Need some expertise help here.
Edit:
I solved this problem using recusrion and memoization. Quite similar question with solution here - https://leetcode.com/problems/minimum-total-distance-traveled/solutions/2816471/recursion-memoization/