题目:

在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。现在,给出提莫对艾希的攻击时间序列和提莫攻击的中毒持续时间,你需要输出艾希的中毒状态总时长。
你可以认为提莫在给定的时间点进行攻击,并立即使艾希处于中毒状态。
示例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. win10 VMware ubuntu12.04 虚拟机不能上网【已解决】

    参考  :链接. 本机环境:Ubuntu 12.04 无线上网(连接手机热点). 主机:Win 10. 步骤1:VMware安装运行后,默认会有3个虚拟网络,VMnet0,VMnet1,VMnet8. ...

  2. Codeforces Round #548 (Div. 2) D 期望dp + 莫比乌斯反演

    https://codeforces.com/contest/1139/problem/D 题意 每次从1,m中选一个数加入队列,假如队列的gcd==1停止,问队列长度的期望 题解 概率正着推,期望反 ...

  3. yyparse() and yylex()

    Yacc 与 Lex 快速入门 yyparse() returns a value of 0 if the input it parses is valid according to the give ...

  4. VS2012智能感知变英文解决办法

    解决办法: 1.从一台没装.NET3.5的机子上复制C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\zh-CN路径下的文件覆盖就可以 2.或者重装中文版的f ...

  5. (转载)Fiddler调式使用知多少(一)深入研究

    原文来源于:http://www.cnblogs.com/tugenhua0707/p/4623317.html,作者:涂根华 !个人觉得原作者写的特别好,故收藏于此 Fiddler调式使用(一)深入 ...

  6. Android 全局搜索条写成自定义控件-曹永思

    图文: 1.Android 自定义控件的布局文件 2.编写Android 自定义控件的要处理的逻辑代码(曹永思) 3.在调用自定义控件的 Activity的布局文件中调用Android 称之为控件,控 ...

  7. 第一章javascript词法结构笔记摘要

    语法介绍 javascript最流行的脚本语言,用于web和HTML,服务器.pc.移动端 轻量级语言,可以插入HTML页面,由浏览器按编写顺序执行 一.字符集 用Unicode字符集编写,是ASCI ...

  8. 所有网卡常用信息获取集中展示(CentOS6 &CentOS7)

    查看所有网卡,状态.光电类型.ip.广播地址.掩码 1.命令如下 ( string='|%-3s|%-18s|%-10s|%-10s|%-10s|%-16s|%-16s|%-16s|'; br=&qu ...

  9. 七种bond模式说明

    第一种模式:mod=0 ,即:(balance-rr) Round-robin policy(平衡抡循环策略) 特点:传输数据包顺序是依次传输(即:第1个包走eth0,下一个包就走eth1….一直循环 ...

  10. Windows 7/Vista下安装Oracle Developer Suit遇到的几个问题

    http://blog.csdn.net/pan_tian/article/details/8016318 Oracle Developer Suite (ODS) 10g是在Windows 7/Vi ...