zoukankan      html  css  js  c++  java
  • LeetCode 495. 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: 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:

    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.

    题目标签:Array

      题目给了我们一个timeSeries, 和一个 duration,让我们找出中毒的total 时间。

      居然和LOL 有关系,没玩过游戏的我,看来也可以去玩几把了。言归正传,这道题目只要设立一个start 和 end,遍历timeSeries,每遇到一个数字的时候,判断这个数字是不是在start 和 end 的范围里面,如果不在的话,直接加duration;如果在的话,只加前面一段的范围;更新start 和 end。注意一下最后还要把结尾的range 加进去。

      补充一下,今天正巧赶上 2017 lol world championships day 2, youtube 看到一个live 直播就点进去看了一下午,看着挺带劲的,以后有机会玩几把。

    Java Solution:

    Runtime beats 77.33% 

    完成日期:09/24/2017

    关键词:Array

    关键点:设立一个范围:start,end 并且维护这个范围

     1 class Solution 
     2 {
     3     public int findPoisonedDuration(int[] timeSeries, int duration) 
     4     {
     5         int totalTime = 0;
     6         if(timeSeries == null || timeSeries.length == 0)
     7             return totalTime;
     8         // set up initial range
     9         int start = timeSeries[0];
    10         int end = start + duration;
    11         // iterate from 2nd 
    12         for(int i=1; i<timeSeries.length; i++)
    13         {    // not overlapped
    14             if(timeSeries[i] > end)
    15                 totalTime += duration; // add duration
    16             else // overlapped
    17                 totalTime += timeSeries[i] - start;
    18             
    19             start = timeSeries[i]; // update start and end
    20             end = start + duration;
    21         }
    22         // add last range
    23         totalTime += end - start;
    24         
    25         return totalTime;
    26     }
    27 }

    参考资料:N/A

    LeetCode 题目列表 - LeetCode Questions List

  • 相关阅读:
    Miracast
    linux软中断与硬中断实现原理概述
    入门视频采集与处理(BT656简介)
    emms指令在MMX指令中的作用
    linux进程的地址空间,核心栈,用户栈,内核线程
    linux 线程的内核栈是独立的还是共享父进程的?
    进程内核栈、用户栈及 Linux 进程栈和线程栈的区别
    Gson JsonParser的使用
    封装JDBC事务操作,执行存储过程测试
    Oracle 存储过程,临时表,动态SQL测试
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7590151.html
Copyright © 2011-2022 走看看