描述

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的更多相关文章

  1. leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解

    dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...

  2. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  3. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  4. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  5. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  6. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  7. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

  8. [LeetCode解题报告] 502. IPO

    题目描述 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最 ...

  9. 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 ...

随机推荐

  1. 数据建模工具------EZMNL

    表结构设计器(EZDML) 表结构设计器EZDML1.5新版本发布,比以前介绍的1.2版本改进了很多,因此重新写了个介绍. 表结构设计,即所谓的数据建模,目前大家常用的同类著名工具有PowerDesi ...

  2. (转)nginx与PHP的关系

    php是一门编程语言,可以编写很多程序,但是只有php的话,你的php只能在你的服务器里孤立的运行,比如你用php写了一个可以通过身高计算人的标准体重的程序,虽然这个程序可以在服务器运行,但是他还不能 ...

  3. 7、注解@Mapper、@MapperScan

    7.注解@Mapper.@MapperScan 2018年09月20日 11:12:41 飞奔的加瓦 阅读数 3284    版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载. https ...

  4. java之struts2之异常处理

    1.在应用项目中,异常的出现时很正常的.而且项目上线后发生异常也很正常的.那么需要对这些异常有相应的处理机制,以便客户能够看你到更加友好的界面.Struts2中提供了异常处理机制. 2.Struts中 ...

  5. Flutter Image(图片)

    Image是一个用于展示图片的组件.支持 JPEG.PNG.GIF.Animated GIF.WebP.Animated WebP.BMP 和 WBMP 等格式. Image 有许多的静态函数: ne ...

  6. 【转载】C#中通过Distinct方法对List集合进行去重

    在C#的List集合对象中,可以使用Distinct方法来对List集合元素进行去重,如果list集合内部元素为值类型,则Distinct方法根据值类型是否相等来判断去重,如果List集合内部元素为引 ...

  7. 用于RISC-V的Makefile示例

    # Initialize ASM For RISC-V .section .text.entry .option norvc .global _start .macro push_reg addi s ...

  8. 在Xcode4 中将iPhone使用的xib转换成iPad使用的xib

    来源:http://blog.3snews.net/space.php?uid=6188&do=blog&id=64200 http://www.giser.net/?p=982 1 ...

  9. java线程的生命周期及五种基本状态

    一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程各重要知识点.掌握了上图中的各知识点,Java中的多线程也就基本上掌 ...

  10. django rest framework的viewset中关于ModelViewset的定义

    ---恢复内容开始--- viewset的关于ModelViewSet的定义是: class ModelViewSet(mixins.CreateModelMixin, mixins.Retrieve ...