leetcode解题报告(32):Teemo Attacking
描述
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.
Example 1:
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.Example 2:
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.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.
分析
这道题并不难,只需要遍历这个时间序列,判断两个时间间隔是否大于等于duration的值,如果是,则将总伤害时间totalDur加上duration;否则,加上时间间隔。
为了避免数组越界,因此数组只遍历到倒数第二个元素,对于最后一个元素,由于是最后一次攻击,因此直接加上duration即可。
代码如下:
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
if(timeSeries.size() == 0) return 0;
int totalDur = 0;
for(int i = 0; i != timeSeries.size() - 1; ++i)
if(timeSeries[i] + duration <= timeSeries[i + 1])totalDur += duration;
else totalDur += (timeSeries[i + 1] - timeSeries[i]);
totalDur += duration;
return totalDur;
}
};
leetcode解题报告(32):Teemo Attacking的更多相关文章
- leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解
dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- [LeetCode解题报告] 502. IPO
题目描述 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最 ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
随机推荐
- Pycharm下直接升级库所遇到的'main'问题
Pycharm下直接升级库所遇到的pip模块中无'main'问题 Author : Benjamin142857 Date : 8/19/2018 对于Pycharm中直接升级库,只需在 \(Sett ...
- Badboy与Jmeter脚本录制
下载地址: http://www.badboy.com.au Badboy 是一个强大的工具,旨在帮助测试和开发复杂的动态应用,Badboy 包括一个简单而全面的捕获/回放界面,强大的 ...
- THUPC&CTS&APIO2019:Far Away
流水账~ THUPC nmdwsmduliu! THUPC Day -INF~Day -2 大概就是自己做题和每周两次的考试,lsy和fcw两个外校的来吊打我们qwqqq THUPC Day -1 Z ...
- 从Harbor仓库拉起镜像,创建容器并更新shell脚本
注意: 此shell脚本仅供基本使用,还有好多待完善的地方 大致流程 使用Jenkins从Gogs拉取仓库代码,根据选择的参数和输入的标签,确定要编译打包jar的模块,以及要制作的docker镜像信息 ...
- vue 生命周期的详解
一.vue生命周期的解析 > 1>什么是vue生命周期 每个vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期.详细来说,就是Vue实例从开始创建,初始化数据, ...
- php ajax生成excel并下载
目标:使用php,通过ajax请求的方式生成一个excel,然后下载. 思路:大致思路是发送一个ajax请求到后台,后台php处理后生成一个excel文件,然后把生成的文件放到一个临时目录,然后把文件 ...
- Oracle表数据转换为XML格式数据
转自:https://blog.csdn.net/smile_caijx/article/details/83352927 使用DBMS_XMLGEN可以解决问题 SELECT DBMS_XMLGEN ...
- mac环境下Android 反编译
连接地址: https://www.jianshu.com/p/3a305f32c4a3
- unity获取ugui上鼠标位置
public class GetMousePos : MonoBehaviour { public Canvas canvas;//画布 private RectTransform rectTrans ...
- 快速为不同 Git 平台配置用户
在 ~ 目录下创建 config 文件可以为项目配置默认的用户信息,但如果希望经常切换,那么最好就是通过命令为项目单独设置用户. 我使用的 shell 是 zsh, 所以我在 ~/.zshrc 文件中 ...