LeetCode 495 Teemo Attacking
来源:互联网 发布:import from es6 node 编辑:程序博客网 时间:2024/06/03 19:44
题目
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo’s attacking ascending time series towards Ashe and the poisoning time duration per Teemo’s attacking, you need to output the total time that Ashe is in poisoned condition.
You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.
- Note:
You may assume the length of given time series array won’t exceed 10000.
You may assume the numbers in the Teemo’s attacking time series and his poisoning time duration per attacking are non-negative integers, which won’t exceed 10,000,000.
Example:
Input: [1,4], 2
Output: 4
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately.
This poisoned status will last 2 seconds until the end of time point 2.
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds.
So you finally need to output 4.
Input: [1,2], 2
Output: 3
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned.
This poisoned status will last 2 seconds until the end of time point 2.
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status.
Since the poisoned status won’t add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3.
So you finally need to output 3.
解法
题目要求:
给一个数组和一个数字,数组中元素是升序的,每一个元素代表释放毒气的时间点,数字表示释放毒气能够中毒的时间,如果在中毒过程中又被攻击了,那么中毒结束时间变为(后一次攻击时间+毒气持续时间),求中毒的总时间。
可以假定给定时间序列数组的长度不超过10000。
可以假定Teemo的攻击时间系列中的数字,每次攻击的中毒时间是非负整数,不超过10,000,000。
用res表示中毒总时间,start表示当次中毒开始时间,end表示当次中毒结束时间。
遍历数组,对于每一个timeSeries[i],如果timeSeries[i]
class Solution {public: int findPoisonedDuration(vector<int>& timeSeries, int duration) { int res = 0; if (timeSeries.size() == 0) return res; int start = timeSeries[0], end = start + duration; for (int i = 0; i < timeSeries.size(); i++) { if (timeSeries[i] <= end) { end = timeSeries[i]+ duration; } else { res += end - start; start = timeSeries[i]; end = start + duration; } } res += end - start; return res; }};
- LeetCode 495 --- Teemo Attacking
- Leetcode 495 Teemo Attacking
- LeetCode 495 Teemo Attacking
- [Leetcode 495] Teemo Attacking
- LeetCode.495 Teemo Attacking
- leetcode 495 Teemo Attacking C++
- leetcode 495[medium]----Teemo Attacking
- LeetCode题解:Teemo Attacking
- LeetCode 495. Teemo Attacking
- LeetCode: Teemo Attacking
- LeetCode 495. Teemo Attacking
- LeetCode:495. Teemo Attacking
- 【Leetcode】495. Teemo Attacking
- [leetcode] 495. Teemo Attacking
- LeetCode|Array|Teemo Attacking
- leetcode 495. Teemo Attacking
- 【LeetCode】495. Teemo Attacking
- LeetCode 495. Teemo Attacking
- Integer 类型与 int 的==比较
- bootstrap提供的h标签重置
- 【XML】 (4)元素与属性
- 3.4
- workerMan学习篇:websocket+workerman聊天功能(三):点对点发送消息模拟
- LeetCode 495 Teemo Attacking
- Fragment全解析系列(一):那些年踩过的坑
- 深入理解非阻塞同步IO和非阻塞异步IO
- Spring事务的传播行为
- xuzuning 版主 xuzuning的想法。
- string初始化小代码欣赏
- Cow Bowling POJ
- STRUTS Action 方法校验
- 软件工程第一周作业