I am stuck on this problem and was wondering if anyone could help me out:
There are n houses on the x-axis {x_1, x_2,…x_n}, I need to find the location on the x-axis that gives me the smallest sum of distances between the houses and the location.
This is trivial of course, but I also need to be able to do it in O(n) time, and I am stuck on the dynamic algorithm.
Edit: Apparently it did not need to be a DP algorithm, which as I said makes it trivial, sorry for the confusion, and thanks for the responses.
I know median finding reasonably well, and I know dynamic programming reasonably well, but I don’t know of any median finding algorithms that I could reasonably construe as DP.
If your x’s were sorted and you didn’t know the median was the answer, I could see computing partial sums from the right and left of a given index as DP-ish sub problems. The optimal solution then minimizes the sum of the right and left partial sums.
But of course, I strongly dislike problems that say, “Solve X with Y”, especially when Y doesn’t really fit. “Solve X, you might want to consider using Y”, is a much better form of problem.