there is $$$N$$$ number of cites in one dimension. the location of the city is $$$a_{i}$$$ you have to place $$$k$$$ ATM such that the sum of all the cities to its nearest ATM distance is minimum.
I don't remember constraints but it was not big.
the question was asked in Honeywell Hiring Challange(closed).
$$$N = 3$$$
$$$A = [1, 2, 3]$$$
$$$K = 1$$$
$$$ans = 2$$$
Thanks in Advance!
I know how to solve this in O(N^2*k)
We will solve it with dp, dp[A][B] denotes the minimal cost to cover the first A buildings with B atms.
Before the general solution for all K, let's think about the solution with K=1. If K=1, than it's optimal to place the atm at the location of the middle — a[N/2]. We can prove this because if there are L buildings to the left of the atm and R buildings to the right, then the cost of moving it right increases by L-R. Since L=R in the middle, moving it right will make the result the same, until we pass a building, from then on L > R, and the cost will only increase.
This approach can be used to compute the cost of covering a subarray of our buildings with one atm. For a subarray L-R, and the middle = M, the cost of covering it with one atm will be sum_of_positions[M...R] — M*number_of_buildings[M...R] + M*number_of_buildings[L...M] — sum_of_positions[L...M]. The sum of positions on a segment can be computed with prefix sums.
Now that we know how to compute the cost of a segment with one atm on it, let's get to the dp transitions: dp[A][B] is the minimum of all dp[X][B-1] + cost_of_segment[X+1...A] for all X < A.
We have N*K states, and each we know how to compute each in O(n) so the overall time complexity will be: O(N^2*K)
Here's some code: