题目:

在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。现在,给出提莫对艾希的攻击时间序列和提莫攻击的中毒持续时间,你需要输出艾希的中毒状态总时长。
你可以认为提莫在给定的时间点进行攻击,并立即使艾希处于中毒状态。
示例1: 输入: [1,4], 2 输出: 4 原因: 在第 1 秒开始时,提莫开始对艾希进行攻击并使其立即中毒。中毒状态会维持 2 秒钟,直到第 2 秒钟结束。
在第 4 秒开始时,提莫再次攻击艾希,使得艾希获得另外 2 秒的中毒时间。
所以最终输出 4 秒。 示例2: 输入: [1,2], 2
输出: 3 原因: 在第 1 秒开始时,提莫开始对艾希进行攻击并使其立即中毒。中毒状态会维持 2 秒钟,直到第 2 秒钟结束。
但是在第 2 秒开始时,提莫再次攻击了已经处于中毒状态的艾希。
由于中毒状态不可叠加,提莫在第 2 秒开始时的这次攻击会在第 3 秒钟结束。
所以最终输出 3。

条件转化

这种方法主要是通过比较时间间距和每次的中毒状态时间,如果时间间距更大艾希就会经历一个完整的中毒状态时间。如果中毒状态时间更长,那么实际上中毒状态还没结束艾希就又中了一次毒,这样只需要加上间隔时间,

用数学表达式来描述就是:

AllTime+=min(Time,duration)

class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
int cnt=0,length=timeSeries.size();
if(length==0)return 0;
for(int i=1;i<length;i++){
int time=timeSeries[i]-timeSeries[i-1];
if(time<duration){
cnt+=time;
}else{
cnt+=duration;
}
}
cnt+=duration;
return cnt;
}
};

一种细微优化的处理方法,比较endTime即结束的时间,这样的优势在于不用像timeSeries[i]-timeSeries[i-1]一样读取内存(参照CSAPP),效率会更高。

class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
int endTime = -1,sum = 0;
for(auto& i:timeSeries){
/*判断timeSeries中的发射时间与上一次中毒失效的时间*/
if(i>=endTime){
sum+=duration;
}
else{
/* waste some time because of the coincide*/
sum+=i+duration-endTime;
}
/* change the endTime*/
endTime=i+duration;
}
return sum;
}
};

[Leetcode]495.提莫攻击的更多相关文章

  1. Java实现 LeetCode 495 提莫攻击

    495. 提莫攻击 在<英雄联盟>的世界中,有一个叫 "提莫" 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和 ...

  2. 495 Teemo Attacking 提莫攻击

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

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

    In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  4. [LeetCode] 495. Teemo Attacking 提莫攻击

    In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  5. LeetCode 495. Teemo Attacking (提莫攻击)

    In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  6. [LeetCode] Teemo Attacking 提莫攻击

    In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  9. LeetCode - 数组遍历

    1. 485. 最大连续 1 的个数 1.1 分析题意 首先:我们求的是连续的1的个数,所以我们不能也没必要对数组进行排序: 其次:只要求求出最大连续1的个数,并不要求具体的区间数目,所以我们只需要用 ...

随机推荐

  1. 2019.02.07 bzoj1487: [HNOI2009]无归岛(仙人掌+树形dp)

    传送门 人脑转化条件过后的题意简述:给你一个仙人掌求最大带权独立集. 思路:跟这题没啥变化好吗?再写一遍加深记忆吧. 就是把每个环提出来分别枚举环在图中的最高点选还是不选分别dpdpdp一下即可,时间 ...

  2. OpenCV(1):显示图像

    显示图像 #include<iostream> #include<opencv2/core/core.hpp> #include<opencv2/highgui/high ...

  3. mac os下提高android studio运行速度终极方法

    /Users/hangliao/ 删除(.android  .gradle)两个文件夹 android studio恢复所有设置到初始化状态,这样会删除已创建的模拟器,所以需从创建一下模拟器 mac ...

  4. boost-使用property_tree来解析xml、json

    property_tree是一个保存了多个属性值的树形数据结构,可以用来解析xml.json.ini.info文件.要使用property_tree和xml解析组件的话需要包含"boost/ ...

  5. HDU 2147 kiki's game (奇偶博弈)

    题意:给定一个 n * m 的格子,从右上角(1, m) 开始每个玩家只能从向下,向左,或者向左下走,谁不能走,谁输. 析:自己做出来,看了网上的几个博客,好像都没说为什么是只有全奇的情况才会输,个人 ...

  6. BT656与BT1120的区别

      从ITU-R BT1120文档上可知,BT1120支持的是1080p: 文档定义一帧为1 125 总行数和1 080 有效行:每行有效像素为1920图像频率60. 50. 30. 25 和 24H ...

  7. AngularJS实战之ng-repeat的详细用法

    一.基本语法 {{$index}}:获取元素的下标. {{$first}}:判断当前元素是否是第一个元素,是则为true,否则:false: {{$last}}:判断当前元素是否是最后一个元素,是则为 ...

  8. 《mysql必知必会》学习_第11章_20180801_欢

    第11章:使用数据处理函数. P69 文本处理函数,upper()函数把文本变成大写字体. select vend_name,upper(vend_name) as vend_name_upcase ...

  9. TF-IDF模型的概率解释

    信息检索概述 信息检索是当前应用十分广泛的一种技术,论文检索.搜索引擎都属于信息检索的范畴.通常,人们把信息检索问题抽象为:在文档集合D上,对于由关键词w[1] … w[k]组成的查询串q,返回一个按 ...

  10. openresty + lua 2、openresty 连接 redis,实现 crud

    redis 的话,openresty 已经集成,ng 的话,自己引入即可. github 地址:https://github.com/openresty/lua-resty-redis github  ...