[Swift]LeetCode881. 救生艇 | Boats to Save People

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-dvuudppy-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

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

Example 1:ide

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

Example 2:spa

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

Example 3:code

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

Note:htm

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

第 i 我的的體重爲 people[i],每艘船能夠承載的最大重量爲 limitblog

每艘船最多可同時載兩人,但條件是這些人的重量之和最多爲 limitget

返回載到每個人所需的最小船數。(保證每一個人都能被船載)。

 

示例 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 }
相關文章
相關標籤/搜索