首页 > 其他 > 详细

leetcode--Teemo Attacking问题

时间:2020-09-27 11:43:00      阅读:43      评论:0      收藏:0      [点我收藏+]
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.

问题大意,英雄Temmo的每次攻击会使得Ashe进入中毒状态,持续时间为duration,而每次攻击将重置中毒时间,也就说Ashe在中毒状态下被Teemo再次攻击

Ashe的中毒时间将再次持续duration,duration之后中毒状态去除。

该问题其实蛮简单的,注意每次间隔是否大于duration即可,唯一该注意的是最后一次攻击。

/**
 * @param {number[]} timeSeries
 * @param {number} duration
 * @return {number}
 */
var findPoisonedDuration = function(timeSeries, duration) {
    var rt = 0;
    for(var i=0;i<timeSeries.length;i++){
        if(i<timeSeries.length-1 && timeSeries[i+1]-timeSeries[i] < duration){
            rt += timeSeries[i+1]-timeSeries[i];
        }else{
            rt += duration;
        }
    }
    return rt;
};

 

leetcode--Teemo Attacking问题

原文:https://www.cnblogs.com/imbasaber/p/13738254.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!