• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode881.救生艇|BoatstoSavePeople

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10602721.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

The i-th person has weight people[i], and each boat can carry a maximum weight of limit.

Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit.

Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.) 

Example 1:

Input: people = 3
Output: 1
Explanation: 1 boat (1, 2)

Example 2:

Input: people = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)

Example 3:

Input: people = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)

Note:

  • 1 <= people.length <= 50000
  • 1 <= people[i] <= limit <= 30000

第 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

664ms

 

 1 class Solution {
 2     func numRescueBoats(_ people: [Int], _ limit: Int) -> Int {
 3         let people = people.sorted()
 4         var left = 0, right = people.count - 1
 5         var count = 0
 6         while left <= right {
 7             if people[left] + people[right] <= limit {
 8                 count += 1
 9                 left += 1
10                 right -= 1
11             } else {
12                 count += 1
13                 right -= 1
14             }
15         }
16         return count        
17     }
18 }

Runtime: 708 ms
Memory Usage: 19.8 MB
 1 class Solution {
 2     func numRescueBoats(_ people: [Int], _ limit: Int) -> Int {
 3         var people = people.sorted(by:<)
 4         var ans:Int = 0
 5         var hi:Int = people.count - 1
 6         var lo:Int = 0 
 7         while (hi >= lo)
 8         {
 9             if people[lo] + people[hi] <= limit
10             {
11                 lo += 1
12             }
13             hi -= 1
14             ans += 1
15         }
16         return ans
17     }
18 }

764ms

 1 class Solution {
 2     func numRescueBoats(_ people: [Int], _ limit: Int) -> Int {
 3         var people = people.sorted()
 4         var ans = 0, headIndex = 0, tailIndex = people.count - 1
 5         while headIndex <= tailIndex {
 6             ans += 1
 7             var lastTailIndex = tailIndex
 8             while tailIndex > headIndex {
 9                 if people[tailIndex]+people[headIndex] > limit {
10                     tailIndex -= 1
11                     if tailIndex == headIndex {
12                         ans += (lastTailIndex - headIndex)
13                         return ans
14                     }
15                 }else {
16                     ans += (lastTailIndex - tailIndex)
17                     tailIndex -= 1
18                     break
19                 }
20             }
21             headIndex += 1
22         }
23         return ans
24     }
25 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap