Loading...
Loading...
You are given an array of doubles locations. You may choose any real number center with -10 <= center <= 10. For each index i, the cost of locations[i] with respect to center is (locations[i] - center)^2 / 10 + (locations[i] - center)^4 / 8000. The average cost is the sum of these costs divided by locations.length.
Return the smallest possible average cost over all allowed choices of center.
1 <= locations.length <= 10^5-10 <= locations[i] <= 10center satisfies -10 <= center <= 10.The only location is 0, so choosing center = 0 makes its cost 0.
The two locations are symmetric around 5, so center = 5 balances them. Each location then has cost 165 / 64, so the average is 165 / 64.
By symmetry, center = 0 balances the two locations. Each location then has cost 45 / 4, so the average is 45 / 4.
The best answer is at center = −2.4454942.