专栏首页眯眯眼猫头鹰的小树杈leetcode495. Teemo Attacking

leetcode495. Teemo Attacking

题目要求

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 1:

Input: [1,4], 2 Output: 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:

  1. You may assume the length of given time series array won't exceed 10000.
  2. 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.

LOL里面有一个英雄名叫Teemo,它的一个技能是在敌人区域释放毒药并且能够持续一段时间。现在传入一个数组,分别表示Teemo释放技能的时间点,以及一个整数表示技能持续的时间,问敌人一共被毒多长时间?要注意,如果在释放技能期间重复释放技能,技能时间是从当前时间开始重新计算的。

思路和代码

简单来说,该技能释放的时间点一共有两种情况:

  1. 无毒
  2. 有毒

如果无毒的话,只需要将技能持续时间累加到总时间上即可。而如果有毒的话,就需要计算额外延长的毒药时间,通过 当前时间+技能持续时间-上一个技能持续时间 得出。代码如下:

public int findPoisonedDuration(int[] timeSeries, int duration) {  
    if (timeSeries == null || timeSeries.length == 0) {  
        return 0;  
    }  
    int totalDuration = 0;  
    int timeLimit = 0;  
    for (int time : timeSeries) {  
        if (timeLimit <= time) {  
            totalDuration += duration;  
        } else {  
            totalDuration += time + duration - timeLimit;  
        }  
        timeLimit = time + duration;  
    }  
    return totalDuration;  
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • leetcode519. Random Flip Matrix

    You are given the number of rows n_rows and number of columns n_cols of a 2D bin...

    眯眯眼的猫头鹰
  • leetcode375. Guess Number Higher or Lower II

    一个猜数字游戏,数字区间为1~n,每猜一次,会有人告诉你猜中了或者当前的数字是大于结果值还是小于结果值。猜对则本次猜测免费,猜错则本次猜测需要花费和数字等额的金...

    眯眯眼的猫头鹰
  • leetcode464. Can I Win

    In the "100 game," two players take turns adding, to a running total, any intege...

    眯眯眼的猫头鹰
  • 聊聊CommonJS与ES6 Module的使用与区别

    学了JS并且用过Node.js后,对模块化应该是有所了解和使用了,那么一定见过以下两种模块导入导出的方式

    前端迷
  • 如何使用SAP事务码SAT进行UI应用的性能分析

    Subject: 【Knowledge share】How to trace UI transaction using SAT

    Jerry Wang
  • Spark Streaming写出文件自定义文件名

    通过重写MultipleOutputFormat来自定义文件名

    俺也想起舞
  • HDU 1023 高精度 卡特兰数

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot...

    csxiaoyao
  • 公交车到站预测1----python处理csv文件

    之前处理csv一直用pandas,今天发现,pandas虽然强大,但是灵活度少了很多,有时候想实现一些简单的功能可能要花很大的代价。大道至简,返璞归真。就像这几...

    钱塘小甲子
  • redis的一些知识-配置文件解释

    redis比较常用,但大部分人都是简单使用一下redis存一些key value,不太关心redis的持久化问题、事务、最大客户端连接数等问题。这一篇就是讲一些...

    天涯泪小武
  • “封杀中兴”后,TensorFlow 等也被“闭源”怎么办?(附中兴最新声明)

    【编者按】从 Oracle “版权战”先后向 Java、JavaScript 开火,到中美贸易摩擦科技战中,中兴可能失去 Android 系统部分服务授权。当我...

    AI科技大本营

扫码关注云+社区

领取腾讯云代金券