资讯专栏INFORMATION COLUMN

[LintCode] Teemo Attacking

whjin / 3335人阅读

Problem

In LOL 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

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.

Solution
public class Solution {
    /**
     * @param timeSeries: the Teemo"s attacking ascending time series towards Ashe
     * @param duration: the poisoning time duration per Teemo"s attacking
     * @return: the total time that Ashe is in poisoned condition
     */
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        // Write your code here
        int total = 0;
        for (int i = 0; i < timeSeries.length; i++) {
            if (i == 0) total += duration;
            else {
                int interval = timeSeries[i] - timeSeries[i-1];
                if (interval < duration) total += interval;
                else total += duration;
            }
        }
        return total;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71401.html

相关文章

  • 【Java】第二章 面向对象

    摘要:类和对象设计英雄这个类有一些共同的状态,比如名字,,护甲,移动速度等等,这样我们就可以设计一种东西,叫做类,代表英雄这样一种事物类英雄状态名字血量,护甲,移动速度这个类没有主方法,不要试图运行它。并不是所有的类都是有主方法的。 1 类和对象 (1) 设计英雄这个类 有一些共同的状态,比如名字,hp,护甲,移动速度等等,这样我们就可以设计一种东西,叫做类,代表英雄这样一种事物 类:英...

    MyFaith 评论0 收藏0
  • cookie和localStorage那些事

    摘要:它的大小限制为左右,是网景公司的前雇员在年月的发明。字符串转义通过来设置的有效期。和的用法和属性允许在浏览器中存储对的数据。用于临时保存同一窗口或标签页的数据,在关闭窗口或标签页之后将会删除这些数据。是浏览器关闭后就立即清除。 一、localStorage、cookie、sessionStorage的区别与练习 showImg(https://segmentfault.com/img/...

    Jeffrrey 评论0 收藏0
  • 剑指offer/LintCode12_最小栈

    摘要:剑指最小栈声明文章均为本人技术笔记,转载请注明出处解题思路实现功能实现一个最小栈,要求操作均为复杂度,解题思路用栈存储数据用最小栈存储中最小元素,保证栈顶元素与栈顶元素同步,表示此时最小值将与此时最小值比较,将更小的一方压栈,保证中栈顶始终 剑指offer/LintCode12_最小栈 声明 文章均为本人技术笔记,转载请注明出处https://segmentfault.com/u/yz...

    Betta 评论0 收藏0
  • 剑指offer/LintCode40_用两个栈模拟队列

    摘要:剑指用两个栈模拟队列声明文章均为本人技术笔记,转载请注明出处解题思路实现功能用两个栈模拟实现一个队列的,和操作解题思路假设有两个栈队列实现始终用入栈实现队列和实现由于依次出栈并压入中,恰好保证中顺序与模拟队列顺序一致,始终保证栈顶元素为模拟 剑指offer/LintCode40_用两个栈模拟队列 声明 文章均为本人技术笔记,转载请注明出处https://segmentfault.com...

    bawn 评论0 收藏0
  • 剑指offer/LintCode494_用两个队列实现一个栈

    摘要:剑指用两个队列实现一个栈声明文章均为本人技术笔记,转载请注明出处解题思路实现功能用两个队列实现一个栈,实现,,和方法解题思路假设有队列和实现栈的操作实现栈操作始终用来入队实现实现栈的方法模拟栈的过程中,保证两个队列中始终有一个队列为空,另一 剑指offer/LintCode494_用两个队列实现一个栈 声明 文章均为本人技术笔记,转载请注明出处https://segmentfault....

    rose 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<