Skip to content

Practice Allocate Mailboxes

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1478
LeetCode Problem

Allocate Mailboxes

Problem Statement

Given the array houses where houses[i] is the location of the ith 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.

Source: Allocate Mailboxes on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
houses = [1,4,8,10,20], k = 3
Output
5

# Example 2

Input
houses = [2,3,5,12,18], k = 2
Output
9

Constraints

1 <= k <= houses.length <= 100
1 <= houses[i] <= 10^4
All the integers of houses are unique.

Related LeetCode Problems

Practice more problems similar to Allocate Mailboxes.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.