495. Teemo Attacking。

来源:互联网 发布:鹏业填报软件 编辑:程序博客网 时间:2024/06/02 07:09

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:

  • You may assume the length of given time series array won’t exceed 10000.
  • 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.

LOL游戏中的提莫,攻击会附加一定时间内的中毒效果。给定一个数组和数值,数值中存存放的是进行攻击的时间点,数值是中毒的持续时间,中毒效果不会叠加,当第一个攻击所造成的中毒效果时间还没有结束就进行了第二次攻击,那么会重新从第二次攻击的时间点开始进行中毒计算。也就是攻击之后,无论之前有没有中毒,都会从当前的时间开始重新刷新毒的效果。

例如:[1,2],2

就是在第1秒的时候进行了攻击,会持续2秒,但是在第2秒的时候又进行了攻击,那么在第2秒的基础上再次持续2秒,所以一共就是3秒。

这题主要需要处理的部分就是叠加的那一部分时间,我们可以用两个值来记录每次中毒开始的时间和理论上结束的时间。

然后开始遍历数组,当发现数组中的攻击时间点并没有超过理论上结束的时间,就说明此时还是中毒状态,让理论上结束的时间点向后增加一个持续的中毒时间,而开始中毒的时间点不需要处理。

如果发现当前的时间点已经大于了理论上结束的时间,说明此刻不是中毒状态了,那么用理论上结束的时间点减去开始中毒的时间点就是已经中毒的时间,此刻让开始中毒的时间等于当前的时间,然后再次计算理论上中毒结束的时间点。

最后遍历完成的时候,还有可能此刻也是中毒状态,所以还需要再次计算一下时间。

例如:[1,3,10],5

中毒开始的时间点为第1秒的时候,理论上会在1+5=6秒的时候结束,但是却发现下一个攻击时间是第3秒,那么相当于是中毒还没有结束就又开始了,此时还是中毒状态,所以中毒开始的时间还是第1秒,因为一直没有中断过。而这次理论上结束的时间在3+5=8秒。然后发现数组中的下一个攻击时间是第10秒,这个时候发现第10秒已经大于了理论上上次中毒的结束时间第8秒,那么说明已经不是中毒时间了,那么就需要计算上一次一共中毒了多少秒,就用上次理论上中毒结束的时间第8秒减去中毒开始的时间第1秒,就是8-1=7秒,上次一共中毒了7秒。因为第10秒进行了攻击,所以中毒的开始时间就从第10秒开始了,下一次理论上结束时间是第10+5=15秒,数组已经结束了,但是中毒状态不清空。然后再次用结束时间减去开始时间就是15-10=5秒,所以一共就是7+5=12秒。

class Solution {public:    int findPoisonedDuration(vector<int>& timeSeries, int duration) {        if(duration == 0 || timeSeries.empty() || timeSeries.size() == 0) {            return 0;        }        int time = 0;        int startTime = timeSeries[0];//记录开始时间        int endTime = timeSeries[0] + duration;//记录结束时间        for(int i=1; i < timeSeries.size(); i++) {            //如果现在的时间大于结束时间,就加上一个间隔时间,并且重置开始时间            if(timeSeries[i] > endTime) {                time += endTime - startTime;                startTime = timeSeries[i];            }            //计算下一个结束时间            endTime = timeSeries[i] + duration;        }        // 计算最后一次结束的时间和开始时间的间隔        time += endTime - startTime;        return time;    }};

上面的思路就是从头开始依次对中毒时间进行相加,其实也可以进行相减。数组中的元素代表着攻击的时间点,那么数组中有多少元素就代表这攻击了多少次,我们先假设每次攻击造成的中毒效果都完成而不会被刷新,那么用每次攻击造成的中毒持续时间乘以数组的长度,就能够得到最大的中毒时间。然后遍历数组,计算当前时间和前一个时间的间隔是不是大于每次中毒造成的持续时间,如果是的话说明上次的中毒效果没有刷新,那么这次攻击我们的假设就成立了;如果当前时间和前一个时间的间隔比每次攻击造成的中毒的持续时间短的话,说明这次的中毒效果会被刷新重新计算,所以需要减去没有继续下去的时间,也就是假设不成立。这样最后减完之后就是中毒的时间。

例如:[1,3],5

先假设每次中毒都都是完全而不被刷新的,也就是每次都会中毒5秒,那么一共就会中毒2*5=10秒。

但是数组的两次攻击时间间隔比较短,本来第1秒攻击完成时,应该是中毒5秒的,但是第3秒的时候就再次攻击了,所以这次间隔是3-1=2秒,也就是才中毒了2秒,但我们的假设是每次都是5秒,有3秒是没有继续下去就被刷新了,所以我们的假设在这两次攻击中不成立,需要减去这3秒,也就是10-3=7秒。

class Solution {public:    int findPoisonedDuration(vector<int>& timeSeries, int duration) {        int time = duration * timeSeries.size();//计算总的时间        for(int i=1; i < timeSeries.size(); i++) {            // 如果两次时间有间隔比较短,应该中毒的时间却没有继续,那么将这部分时间减去            time -= max(0,duration - (timeSeries[i] - timeSeries[i-1]));        }        return time;    }};
原创粉丝点击