[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的个数,并不要求具体的区间数目,所以我们只需要用 ...
随机推荐
- 2018.10.26 poj3421X-factor Chains(数论+排列组合)
传送门 排列组合入门题. 令X=p1a1p2a2..pkakX=p_1^{a_1}p_2^{a_2}..p_k^{a_k}X=p1a1p2a2..pkak 那么答案1就等于∑i=1kai\ ...
- springboot 增加过滤器方法
在访问服务器时,我们需要控制用户是否允许权限,这个时候可以使用过滤器. 在springboot 配置过滤器的方法如下: 编写过滤器代码: package com.neo.filter; import ...
- mysql 配置MHA
在配置好GTID复制后,配置MHA的高可用. 1.准备3台机器 192.168.31.100 主库 192.168.31.101 从库 192.168.31.104 从库 MHA控制节点 2.设置SS ...
- Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) D. Minimum path(字典序)
https://codeforces.com/contest/1072/problem/D 题意 给你一个n*n充满小写字母的矩阵,你可以更改任意k个格子的字符,然后输出字典序最小的从[1,1]到[n ...
- openstack网络基本概念(转)
OpenStack的Neutron能够管理OpenStack环境中的虚拟 网络基础设施(VNI).和物理网络基础设施(PNI). OpenStack的Neutron同意租户创建虚拟网络拓扑结构.包括的 ...
- Jquery中的事件命名机制
来源:aitangyong的专栏 JQuery中的bind()和unbind(),提供了事件的绑定和取消机制,既可以绑定html默认支持的事件,也能够绑定自定义的事件.JQuery支持自定义事件,这显 ...
- struts2访问web资源
通过ActionContext访问 public class TestActionContextAction { public String execute(){ //获取 ActionContext ...
- XML xmlns
xmlns xml namespaces 参考 http://www.w3school.com.cn/tags/tag_prop_xmlns.asp http://www.w3school.com.c ...
- MFC模块状态(二)AFX_MANAGE_STATE(AfxGetStaticModuleState())
以前写MFC的DLL的时候,总会在自动生成的代码框架里看到提示,需要在每一个输出的函数开始添加上AFX_MANAGE_STATE(AfxGetStaticModuleState()).一直不明白这样做 ...
- GPT分区在IBM服务器上安装linux不能引导的解决方法
提示: Your boot partition is on a disk using the GPT partitioning Scheme but this machines cannot boot ...