Dynamic Programing - City Planning. A city planner is asked to organize grocery shops in a new city. The city has a straight line main street that goes throughout the city. The city planner is asked to position where should the city provide permits to build new grocery shops so that people of the new city can have the shortest distance to their grocery markets. The population density is not constant along both sides of the main street. A higher density is around multiple city centers or crossroads along side the main street.
Your task is to develop an algorithm, given the positions of the city centers and the number of grocery shops, computes the least possible sum of all distances between each city centers and its nearest grocery shop.
Input to your Algorithm:
Output of your algorithm: A single integer, which is the sum of all distances between each city center and its nearest grocery market.
Sample Input:
5
[1 2 3 6 7 9 11 21 40 50]
Sample Output:
9
Figure 1: A visualization of City Center Positions and Grocery Shops. (Numbers in grocery shop row is the distance to the nearest shop. )
Tasks:
Comments
Leave a comment