Find weighted median in an array — meeting problem

By | December 20, 2014
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

Problem description:
Assume in x-coodinate, there are  cities. The city i locates at xi position, and has pi people. Now people in those cities want to find a place to have a meeting. We should find the location l, where it spend least cost for all people(make the following formula smallest).

We know how to find the median. Just get the middle number. For example, 1 2 3 5 6, the median is 3.
What if each number has a weight?
For example numbers: 1 2 3 5 6
weights:1 3 2 2 5
In this case, we just discrete the numbers by its weight. So, the number sequence will become:
1 2 2 2 3 3 5 5 6 6 6 6 6
Find the median of the new sequence, which is 5.
Accordingly, we solved the meeting problem above.