mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
52 lines
1.9 KiB
HTML
52 lines
1.9 KiB
HTML
<p>给你一个房屋数组<code>houses</code> 和一个整数 <code>k</code> ,其中 <code>houses[i]</code> 是第 <code>i</code> 栋房子在一条街上的位置,现需要在这条街上安排 <code>k</code> 个邮筒。</p>
|
||
|
||
<p>请你返回每栋房子与离它最近的邮筒之间的距离的 <strong>最小 </strong>总和。</p>
|
||
|
||
<p>答案保证在 32 位有符号整数范围以内。</p>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>示例 1:</strong></p>
|
||
|
||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/06/13/sample_11_1816.png" style="height: 154px; width: 454px;"></p>
|
||
|
||
<pre><strong>输入:</strong>houses = [1,4,8,10,20], k = 3
|
||
<strong>输出:</strong>5
|
||
<strong>解释:</strong>将邮筒分别安放在位置 3, 9 和 20 处。
|
||
每个房子到最近邮筒的距离和为 |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5 。
|
||
</pre>
|
||
|
||
<p><strong>示例 2:</strong></p>
|
||
|
||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/06/13/sample_2_1816.png" style="height: 154px; width: 433px;"></strong></p>
|
||
|
||
<pre><strong>输入:</strong>houses = [2,3,5,12,18], k = 2
|
||
<strong>输出:</strong>9
|
||
<strong>解释:</strong>将邮筒分别安放在位置 3 和 14 处。
|
||
每个房子到最近邮筒距离和为 |2-3| + |3-3| + |5-3| + |12-14| + |18-14| = 9 。
|
||
</pre>
|
||
|
||
<p><strong>示例 3:</strong></p>
|
||
|
||
<pre><strong>输入:</strong>houses = [7,4,6,1], k = 1
|
||
<strong>输出:</strong>8
|
||
</pre>
|
||
|
||
<p><strong>示例 4:</strong></p>
|
||
|
||
<pre><strong>输入:</strong>houses = [3,6,14,10], k = 4
|
||
<strong>输出:</strong>0
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>n == houses.length</code></li>
|
||
<li><code>1 <= n <= 100</code></li>
|
||
<li><code>1 <= houses[i] <= 10^4</code></li>
|
||
<li><code>1 <= k <= n</code></li>
|
||
<li>数组 <code>houses</code> 中的整数互不相同。</li>
|
||
</ul>
|