[Leetcode]495.提莫攻击
题目:
在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。现在,给出提莫对艾希的攻击时间序列和提莫攻击的中毒持续时间,你需要输出艾希的中毒状态总时长。
你可以认为提莫在给定的时间点进行攻击,并立即使艾希处于中毒状态。
示例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.提莫攻击的更多相关文章
- Java实现 LeetCode 495 提莫攻击
495. 提莫攻击 在<英雄联盟>的世界中,有一个叫 "提莫" 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和 ...
- 495 Teemo Attacking 提莫攻击
在<英雄联盟>的世界中,有一个叫“提莫”的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和提莫攻击的中毒持续时间,你需要输出艾希的中毒 ...
- [Swift]LeetCode495. 提莫攻击 | Teemo Attacking
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- [LeetCode] 495. Teemo Attacking 提莫攻击
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- LeetCode 495. Teemo Attacking (提莫攻击)
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- [LeetCode] Teemo Attacking 提莫攻击
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode - 数组遍历
1. 485. 最大连续 1 的个数 1.1 分析题意 首先:我们求的是连续的1的个数,所以我们不能也没必要对数组进行排序: 其次:只要求求出最大连续1的个数,并不要求具体的区间数目,所以我们只需要用 ...
随机推荐
- Echarts的使用方法
效果图: 1. 在echarts官网下载包,解压后,将文件Echarts\echarts-2.2.7\echarts-2.2.7\doc\example\www\js\echarts.js文件拷贝,放 ...
- 检索 COM 类工厂中 CLSID 为 {10021F00-E260-11CF-AE68-00AA004A34D5} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))。
ASP.NET利用SQLDMO可以实现在线备份.还原数据库等各种功能. 由于客户的数据库和WEB服务不再同一台服务器,把网站部署在服务器上以后,运行程序,提示如下错误 当使用Interop.SQLDM ...
- DOM中的事件对象和IE事件对象
DOM中的事件对象 IE事件对象 属性/方法 类型 读/写 说明 属性/方法 类型 读/写 说明 bubles Boolean 只读 表明事件是否冒泡 cancleBubble Boolean ...
- java IODemo
关键代码: RandomAccessFile file = new RandomAccessFile("temp.dat", "rw"); fi ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记六之铭文升级版
铭文一级: 整合Flume和Kafka的综合使用 avro-memory-kafka.conf avro-memory-kafka.sources = avro-sourceavro-memory-k ...
- 大压力下Redis参数调整要点
调整以下参数,可以大幅度改善Redis集群的稳定性: 为何大压力下要这样调整? 最重要的原因之一Redis的主从复制,两者复制共享同一线程,虽然是异步复制的,但因为是单线程,所以也十分有限.如果主从间 ...
- __cxa_call_unexpected原因
coredump的调用栈: #0 0xf76f5440 in __kernel_vsyscall () #1 0xf73c4657 in raise () from /lib/libc.so.6 ...
- 1.7.8使用return 停止线程
package com.cky.thread; /** * Created by edison on 2017/12/3. */ public class MyThread12 extends Thr ...
- Python之turtle库
在命令行下```python -m pip install turtle``` 大致有两种命令: 运动命令: forward(distance) #向前移动距离distance代表距离 backwar ...
- 类中main函数的快捷创建
方法一: 新建类时,选择创建 方法二: 打出main-->Alt + /