zoukankan      html  css  js  c++  java
  • 495. Teemo Attacking

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

    思路

    Solution (C++):

    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
        if (timeSeries.empty())  return 0;
        int n = timeSeries.size(), res = 0, end = 0;
        
        for (int i = 0; i < n; ++i) {
            res += duration - (end > timeSeries[i]) * (end - timeSeries[i]);
            end = timeSeries[i] + duration;
        }
        return res;
    }
    

    性能

    Runtime: 60 ms  Memory Usage: 8.8 MB

    思路

    Solution (C++):

    
    

    性能

    Runtime: ms  Memory Usage: MB

    相关链接如下:

    知乎:littledy

    欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

    作者:littledy
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    .net做UG NX二次开发(VB.net) / NX Secondry Dev. with .net using VB.net Code
    UGS Object ID 工具 [开源]
    Build a NX Open .NET application
    Open C、Open C++和NXOpen C++混合开发
    UG二次开发技术的研究
    ufun视频教程
    互联网上的阴暗战争
    谈谈UG二次开发的Open和NXOpen
    IBM云计算平台下 Windows Server 2003虚拟机的使用
    使用IBM云计算平台虚拟机
  • 原文地址:https://www.cnblogs.com/dysjtu1995/p/12702501.html
Copyright © 2011-2022 走看看