Math proof: taking median as the center
index of 1s\' forms an increasing array\ntotal k points in the array \nWhat\'s the min cost for collecting them together?\n  di= distance bewteen i and i+1 \n (
index of 1s\' forms an increasing array\ntotal `k` points in the array \nWhat\'s the min cost for collecting them together?\n  ` di`= distance bewteen `i` and `i+1` \n ` ( for example : [1,5,7] d0 = 4, d1 = 2)`\n  pick `j` as the center\n  `cost = centralizing_cost(move all num to center j) - save `\n ` ( for example : center = 5 , cost for 7 = 2 - 1)`\n \n  let\'s say `k = 5 (i from 0 to 4) `\n  0--(d0)--1--(d1)--2--(d2)--**3**--(d3)--4\n  take center j = 3\n `centralizing_cost = left_cost + right_cost = (1 * d0 +2 * d1 + 3 * d2)  + (1 * d3)`\n `save = left_save + right_save = (3 + 2 + 1) + (1) = (1 + 2 + 3) + (1)`\n` cost =   (1 * (d0-1) +2 * (d1-1) + 3 * (d2-1))  + (1 * (d3-1)) =  (1 * D0 +2 * D1 + 3 * D2)  + (1 * D3)`\nwhere` Di = di - 1 `\nso we have **cost = centralizing_cost (distance is shorten by 1)**\nmin_cost happens when centralizing_cost is minimized.\ncentralizing_cost is determined by the coefficent for each Di ,and the minimal coefficents are \n`1, 2, 3...m-2, m-1, m-1, m-2,...3, 2, 1`\nThis is when **center = median**\n\n```\ndef minMoves(self, nums: List[int], k: int) -> int:\n        position = [i for i,num in enumerate(nums) if num == 1]\n        left,right = k//2, k -  k//2 \n\t\t\n\t\t#init cost for the first window\n        cur = sum(abs(position[i] - (position[left]-left) - i) for i in range(k))\n        res = cur\n            for i in range(left+1,len(position)-right+1): #i : index of median\n                # new : cost for the new number\n                new = position[i+right-1] - position[i] - right + 1\n                # old : cost for the number dropped\n                old = position[i-1] - position[i-1-left] - left\n                #same : modified cost for same elements\n                same = (position[i] - position[i-1] - 1) * ( left-(right-1))\n                #cost for new window = cost for previous window + new - old + same\n                cur += same + new - old\n                res = min(res,cur)\n            return res\n```
[]