[Swift]LeetCode495. 提莫攻擊 | Teemo Attacking

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

In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition.git

You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.github

Example 1:數組

Input: [1,4], 2
Output: 4
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately. 
This poisoned status will last 2 seconds until the end of time point 2.
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds.
So you finally need to output 4.

Example 2:微信

Input: [1,2], 2
Output: 3
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned. 
This poisoned status will last 2 seconds until the end of time point 2.
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status.
Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3.
So you finally need to output 3.

Note:spa

  1. You may assume the length of given time series array won't exceed 10000.
  2. You may assume the numbers in the Teemo's attacking time series and his poisoning time duration per attacking are non-negative integers, which won't exceed 10,000,000.

在《英雄聯盟》的世界中,有一個叫 「提莫」 的英雄,他的攻擊能夠讓敵方英雄艾希(編者注:寒冰射手)進入中毒狀態。如今,給出提莫對艾希的攻擊時間序列和提莫攻擊的中毒持續時間,你須要輸出艾希的中毒狀態總時長。code

你能夠認爲提莫在給定的時間點進行攻擊,並當即使艾希處於中毒狀態。htm

示例1:blog

輸入: [1,4], 2
輸出: 4
緣由: 在第 1 秒開始時,提莫開始對艾希進行攻擊並使其當即中毒。中毒狀態會維持 2 秒鐘,直到第 2 秒鐘結束。
在第 4 秒開始時,提莫再次攻擊艾希,使得艾希得到另外 2 秒的中毒時間。
因此最終輸出 4 秒。

示例2:ci

輸入: [1,2], 2
輸出: 3
緣由: 在第 1 秒開始時,提莫開始對艾希進行攻擊並使其當即中毒。中毒狀態會維持 2 秒鐘,直到第 2 秒鐘結束。
可是在第 2 秒開始時,提莫再次攻擊了已經處於中毒狀態的艾希。
因爲中毒狀態不可疊加,提莫在第 2 秒開始時的此次攻擊會在第 3 秒鐘結束。
因此最終輸出 3。

注意:

  1. 你能夠假定時間序列數組的總長度不超過 10000。
  2. 你能夠假定提莫攻擊時間序列中的數字和提莫攻擊的中毒持續時間都是非負整數,而且不超過 10,000,000。

64ms

 1 class Solution {
 2     func findPoisonedDuration(_ timeSeries: [Int], _ duration: Int) -> Int {
 3         let count = timeSeries.count
 4         guard count > 0 else {
 5             return 0
 6         }
 7         
 8         var left = timeSeries[0]
 9         var right = timeSeries[0]
10         var total = 0
11         
12         for index in 1..<count {
13             if right + duration < timeSeries[index] {
14                 total += right - left + duration
15                 left = timeSeries[index]
16             }
17             right = timeSeries[index]
18         }
19         total += right - left + duration
20     
21         return total
22     }
23 }

388ms

 1 class Solution {
 2     func findPoisonedDuration(_ timeSeries: [Int], _ duration: Int) -> Int {
 3         guard timeSeries.count > 0 else { return 0 }
 4         var totalDuration = duration
 5         for index in 1..<timeSeries.count {
 6             var lastMax = timeSeries[index - 1] + duration - 1
 7             if lastMax < timeSeries[index] {
 8                 totalDuration += duration
 9             } else {
10                 let currMax = timeSeries[index] + duration - 1
11                 totalDuration += (currMax - lastMax)
12             }
13         }
14         return totalDuration
15     }
16 }

392ms

 1 class Solution {
 2     func findPoisonedDuration(_ timeSeries: [Int], _ duration: Int) -> Int {
 3         if timeSeries.isEmpty {return 0}
 4         var res:Int = 0
 5         var n:Int = timeSeries.count
 6         for i in 1..<n
 7         {
 8             var diff:Int = timeSeries[i] - timeSeries[i - 1]
 9             res += (diff < duration) ? diff : duration
10         }
11         return res + duration
12     }
13 }

392ms

 1 class Solution {
 2     func findPoisonedDuration(_ timeSeries: [Int], _ duration: Int) -> Int {
 3         if timeSeries.isEmpty {return 0}
 4         var res:Int = 0
 5         var n:Int = timeSeries.count
 6         for i in 1..<n
 7         {
 8             var diff:Int = timeSeries[i] - timeSeries[i - 1]
 9             res += (diff < duration) ? diff : duration
10         }
11         return res + duration
12     }
13 }

396ms

 1 class Solution {
 2 func findPoisonedDuration(_ timeSeries: [Int], _ duration: Int) -> Int {
 3     if timeSeries.count == 0 {
 4         return 0
 5     }
 6     if timeSeries.count == 1 {
 7         return duration
 8     }
 9     
10     var result = 0
11     
12     for i in 0..<timeSeries.count-1 {
13         if timeSeries[i] + duration <= timeSeries[i + 1] {
14             result += duration
15         }
16         else {
17             result += (timeSeries[i + 1] - timeSeries[i])
18         }
19     }
20     
21     result += duration
22     
23     return result
24   }
25 }

440ms

 1 class Solution {
 2     func findPoisonedDuration(_ timeSeries: [Int], _ duration: Int) -> Int {
 3         var res = 0, start = -1, end = -1
 4         for t in timeSeries.sorted() {
 5             if t > end {
 6                 res += end - start
 7                 start = t
 8             }
 9             end = t + duration
10         }
11         return res + end - start
12     }
13 }
相關文章
相關標籤/搜索