Considering a lane to be one-dimensional, you are provided with an array police_station
consisting of n
distinct integers representing the coordinates of police stations.
You, as a prosperous individual, aspire to acquire exactly capacity
distinct properties across the entirety of this one-dimensional lane (which may be located anywhere on the number line), with the condition that they do not coincide with the coordinates of any police station and neither any older property acquired.
The cost associated with purchasing a property is determined by its minimum distance from any police station. The total cost incurred will be the sum of costs associated with purchasing each property.
Here distance between x1
and x2
means the absolute difference of x1
and x2
(i.e. |x1-x2|
)
Given police_station
an array containing the coordinates of the police stations, and capacity
indicates the total number of distinct properties you can acquire. Your task is to calculate the total minimum cost to acquire exactly the capacity
number of properties.
Function Description
Complete the function minAcquireCost
in the editor.
minAcquireCost
takes the following arguments:
int police_station[n]
: the input array police station containing the coordinates of all the police stations.int capacity
: the total number of distinct properties you can acquire
Returns
int: the total minimum cost to acquire exactly the capacity
number of properties
Example 1:
Input: police_station = [7, 8], capacity = 4
Output: 6
Explanation:Supposen=2
,police_station = [7, 8]
, andcapacity = 4
. The coordinates of your property will be[5, 6, 9, 10]
for minimum cost. So, the answer is sum of|7 - 5| + |7 - 6| + |8 - 9| + |8 - 10| = 6
.
An unknown urban legend for now ๐ฆ

input:
output: