题目:

在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。现在,给出提莫对艾希的攻击时间序列和提莫攻击的中毒持续时间,你需要输出艾希的中毒状态总时长。
你可以认为提莫在给定的时间点进行攻击,并立即使艾希处于中毒状态。
示例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. 2018.11.30 bzoj3230: 相似子串(后缀数组)

    传送门 后缀数组入门题. 建立正反两个后缀数组算就行了. 代码: #include<bits/stdc++.h> #define ri register int using namespa ...

  2. 2018.06.27The Windy's(费用流)

    The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6003 Accepted: 2484 Descripti ...

  3. idea在哪执行maven clean?

  4. warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY

    问题: yum安装软件时候报如下错误: warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOK ...

  5. ibatis注意要点

    一.ibatis的关键字like查询 select * from t_student where s_name '%张%'; 这种like语句在ibatis中怎么写,他们现在的项目是用ibatis作为 ...

  6. git 版本库拆分和subtree用法

    git 版本库拆分 原文地址: https://segmentfault.com/a/1190000002548731 程序员最爽的事情是什么?删删删!所有项目本来都很苗条的,时间长了难免有一些越搞越 ...

  7. Effective C++ 随笔(3)

    条款12: 以对象管理资源 两种只能指针: std:auto_ptr<> 当使用copy操作室,原先的智能指针指向为null std:tr1:share_ptr<int> sp ...

  8. linux下运算的几种方法

    1.expr 1.1 最简单的用法 yan@yan:~$ \* yan@yan:~$ 1.2 bash脚本对于expr yan@yan:~$ cat expr1.sh #!/bin/bash PATH ...

  9. 知识点:CSS代码语法

    css 样式由选择符和声明组成,而声明又由属性和值组成,如下图所示: 选择符:又称选择器,指明网页中要应用样式规则的元素,如本例中是网页中所有的段(p)的文字将变成蓝色,而其他的元素(如ol)不会受到 ...

  10. Linux入门(9)——Ubuntu16.04安装flash player

    打开网页经常提示安装flash player,不安装flash player很多网页的视频都看不了. Ubuntu16.04安装flash player 打开终端,输入: sudo apt-get i ...