881:救生艇
This commit is contained in:
parent
433ef438ba
commit
58ccc2c64c
68
src/main/java/leetcode/editor/cn/BoatsToSavePeople.java
Normal file
68
src/main/java/leetcode/editor/cn/BoatsToSavePeople.java
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
//第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit。
|
||||||
|
//
|
||||||
|
// 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit。
|
||||||
|
//
|
||||||
|
// 返回载到每一个人所需的最小船数。(保证每个人都能被船载)。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入:people = [1,2], limit = 3
|
||||||
|
//输出:1
|
||||||
|
//解释:1 艘船载 (1, 2)
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入:people = [3,2,2,1], limit = 3
|
||||||
|
//输出:3
|
||||||
|
//解释:3 艘船分别载 (1, 2), (2) 和 (3)
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
// 输入:people = [3,5,3,4], limit = 5
|
||||||
|
//输出:4
|
||||||
|
//解释:4 艘船分别载 (3), (3), (4), (5)
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= people.length <= 50000
|
||||||
|
// 1 <= people[i] <= limit <= 30000
|
||||||
|
//
|
||||||
|
// Related Topics 贪心 数组 双指针 排序 👍 151 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
//881:救生艇
|
||||||
|
class BoatsToSavePeople {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new BoatsToSavePeople().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int numRescueBoats(int[] people, int limit) {
|
||||||
|
Arrays.sort(people);
|
||||||
|
int count = 0;
|
||||||
|
int start = 0;
|
||||||
|
int end = people.length - 1;
|
||||||
|
while (start < end) {
|
||||||
|
if (people[end] < limit && people[start] + people[end] <= limit) {
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
return start == end ? count + 1 : count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
<p>第 <code>i</code> 个人的体重为 <code>people[i]</code>,每艘船可以承载的最大重量为 <code>limit</code>。</p>
|
||||||
|
|
||||||
|
<p>每艘船最多可同时载两人,但条件是这些人的重量之和最多为 <code>limit</code>。</p>
|
||||||
|
|
||||||
|
<p>返回载到每一个人所需的最小船数。(保证每个人都能被船载)。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>people = [1,2], limit = 3
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
<strong>解释:</strong>1 艘船载 (1, 2)
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>people = [3,2,2,1], limit = 3
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
<strong>解释:</strong>3 艘船分别载 (1, 2), (2) 和 (3)
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>people = [3,5,3,4], limit = 5
|
||||||
|
<strong>输出:</strong>4
|
||||||
|
<strong>解释:</strong>4 艘船分别载 (3), (3), (4), (5)</pre>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= people.length <= 50000</code></li>
|
||||||
|
<li><code>1 <= people[i] <= limit <= 30000</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>双指针</li><li>排序</li></div></div><br><div><li>👍 151</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user