1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/安排邮筒 [allocate-mailboxes].html
2022-03-29 12:43:11 +08:00

52 lines
1.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个房屋数组<code>houses</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;,其中&nbsp;<code>houses[i]</code>&nbsp;是第 <code>i</code>&nbsp;栋房子在一条街上的位置,现需要在这条街上安排 <code>k</code>&nbsp;个邮筒。</p>
<p>请你返回每栋房子与离它最近的邮筒之间的距离的 <strong>最小 </strong>总和。</p>
<p>答案保证在 32 位有符号整数范围以内。</p>
<p>&nbsp;</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>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == houses.length</code></li>
<li><code>1 &lt;= n&nbsp;&lt;= 100</code></li>
<li><code>1 &lt;= houses[i] &lt;= 10^4</code></li>
<li><code>1 &lt;= k &lt;= n</code></li>
<li>数组&nbsp;<code>houses</code>&nbsp;中的整数互不相同。</li>
</ul>