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

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.

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

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:

  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.


在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。现在,给出提莫对艾希的攻击时间序列和提莫攻击的中毒持续时间,你需要输出艾希的中毒状态总时长。

你可以认为提莫在给定的时间点进行攻击,并立即使艾希处于中毒状态。

示例1:

输入: [1,4], 2
输出: 4
原因: 在第 1 秒开始时,提莫开始对艾希进行攻击并使其立即中毒。中毒状态会维持 2 秒钟,直到第 2 秒钟结束。
在第 4 秒开始时,提莫再次攻击艾希,使得艾希获得另外 2 秒的中毒时间。
所以最终输出 4 秒。

示例2:

输入: [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 }

原文地址:https://www.cnblogs.com/strengthen/p/10370594.html

时间: 2024-11-09 00:48:24

[Swift]LeetCode495. 提莫攻击 | Teemo Attacking的相关文章

495. 提莫攻击 Teemo Attacking

In LLP 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 outp

Teemo Attacking

In LLP 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 outp

495. Teemo Attacking

In LLP 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 outp

LeetCode 495. Teemo Attacking(medium)

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 outp

(Medium) Teemo Attacking - LeetCode

Description: 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

LeetCode &quot;Teemo Attacking&quot;

Isn't it 'Easy'? class Solution { public: int findPoisonedDuration(vector<int>& ts, int d) { int ret = 0, s = 0, e = 0; for(auto t : ts) { if(t > e) { ret += e - s; s = t; } e = t + d; } ret += e - s; return ret; } };

LeetCode 495. 提莫攻击

因为时间序列有序,从后往前遍历一次,总时间要么是前一个时间点减后一个时间点,要么是吃满中毒持续时间. class Solution { public: int findPoisonedDuration(vector<int>& timeSeries, int duration) { int count = 0; for(int i = timeSeries.size()-1; i>=0; i--) { if(i == (timeSeries.size()-1)) count =

[LeetCode] Dota2 Senate 刀塔二参议院

In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-bas

面向对象text 01 盖伦vs瑞文vs提莫

''' Text For Class: League of Legends Garen vs Riven vs Teemo ''' import random # 全局随机 import time class HeroName: # 英雄名字类 GameName = 'League of Legends' # 游戏名字 def __init__(self,hero_name): # 初始化设定,自身和形参名 self.hero_name = hero_name # 形参名初始化 self.HP