zoukankan      html  css  js  c++  java
  • [Swift]LeetCode881. 救生艇 | Boats to Save People

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

    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 = [1,2], limit = 3
    Output: 1
    Explanation: 1 boat (1, 2)
    

    Example 2:

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

    Example 3:

    Input: people = [3,5,3,4], limit = 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 }
  • 相关阅读:
    MSP430程序库<十四>DMA程序库
    MSP430程序库<十三>硬件乘法器使用
    MSP430程序库<十五>Flash控制器
    MSP430程序库<九>数码管显示
    [debug] 调试小结
    SourceInsight Shortcuts
    git commands
    Linux常用命令
    [转] 宏点滴
    Linux 开发
  • 原文地址:https://www.cnblogs.com/strengthen/p/10602721.html
Copyright © 2011-2022 走看看