Class Solution
java.lang.Object
g1401_1500.s1478_allocate_mailboxes.Solution
1478 - Allocate Mailboxes.<p>Hard</p>
<p>Given the array <code>houses</code> where <code>houses[i]</code> is the location of the <code>i<sup>th</sup></code> house along a street and an integer <code>k</code>, allocate <code>k</code> mailboxes in the street.</p>
<p>Return <em>the <strong>minimum</strong> total distance between each house and its nearest mailbox</em>.</p>
<p>The test cases are generated so that the answer fits in a 32-bit integer.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/05/07/sample_11_1816.png" alt="" /></p>
<p><strong>Input:</strong> houses = [1,4,8,10,20], k = 3</p>
<p><strong>Output:</strong> 5</p>
<p><strong>Explanation:</strong> Allocate mailboxes in position 3, 9 and 20. Minimum total distance from each houses to nearest mailboxes is |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/05/07/sample_2_1816.png" alt="" /></p>
<p><strong>Input:</strong> houses = [2,3,5,12,18], k = 2</p>
<p><strong>Output:</strong> 9</p>
<p><strong>Explanation:</strong> Allocate mailboxes in position 3 and 14. Minimum total distance from each houses to nearest mailboxes is |2-3| + |3-3| + |5-3| + |12-14| + |18-14| = 9.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= houses.length <= 100</code></li>
<li><code>1 <= houses[i] <= 10<sup>4</sup></code></li>
<li>All the integers of <code>houses</code> are <strong>unique</strong>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minDistance
public int minDistance(int[] houses, int k)
-