Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    1478 - Allocate Mailboxes\.

    Hard

    Given the array houses where houses[i] is the location of the <code>i<sup>th</sup></code> house along a street and an integer k, allocate k mailboxes in the street.

    Return the minimum total distance between each house and its nearest mailbox.

    The test cases are generated so that the answer fits in a 32-bit integer.

    Example 1:

    Input: houses = 1,4,8,10,20, k = 3

    Output: 5

    Explanation: 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

    Example 2:

    Input: houses = 2,3,5,12,18, k = 2

    Output: 9

    Explanation: 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.

    Constraints:

    • 1 &lt;= k &lt;= houses.length &lt;= 100

    • <code>1 <= housesi<= 10<sup>4</sup></code>

    • All the integers of houses are unique.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer minDistance(IntArray houses, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait