leetcode 495. Teemo Attacking 提莫攻击 + 计算间隔-程序员宅基地

技术标签: leetcode For C++  leetcode For Java  

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

本题题意有点绕,但是看了给的例子就可以发现,就是计算间隔的总和,很简单的

代码如下:

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <cmath>

using namespace std;

class Solution 
{
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration)
    {
        if (timeSeries.size() <=0)
            return 0;

        int sum = 0;
        for (int i = 1; i < timeSeries.size(); i++)
        {
            if (timeSeries[i] - timeSeries[i - 1] >= duration)
                sum += duration;
            else
                sum += timeSeries[i] - timeSeries[i - 1];
        }
        sum += duration;
        return sum;
    }
};
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/JackZhang_123/article/details/78792439

智能推荐

修改profile文件时提示只读的解决办法-程序员宅基地

文章浏览阅读1.1w次,点赞3次,收藏9次。3、A:linux恢复模式下修改profile文件报只读错误 安了个ubuntu,今天装了个JDK环境,配置环境变量时,我修改的是/etc/profile文件,但被我改错了,把PATH那个环境变量改错了,..._profile只读

Halcon学习笔记----region_to_bin算子详解-程序员宅基地

文章浏览阅读9.4k次,点赞3次,收藏16次。今天终于解决了困扰我很久的一个问题,在VC中调用HALCON中的分割函数后,在最后返回显示时总是报错,让我郁闷了很久,Undefined gray in get_image_pointer3 或Undefined gray in get_image_pointer。 原来问题出在对于bin_threshold、threshold等这些分割函数的返回值上面,把返回值当成Imag_region_to_bin

微信公众号开发之获得素材列表_csdn服务商获取公众号素材-程序员宅基地

文章浏览阅读1k次。微信公众号开发之获得素材列表对于获得公众号的素材列表首先需要阅读微信开发者手册开发准备工具测试公众号内网穿透HTTP地址(微信对本地服务的通信对一些,公众号信息的获取不需要)具体参考:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QgOcoCTL-1625189967278)(C:\Users\wang\Ap_csdn服务商获取公众号素材

使用rz命令从本地上传文件到linux服务器卡住问题_putty rz上传文件没反应-程序员宅基地

文章浏览阅读3.2k次。问题背景:使用putty进行ssh远程连接,上传文件时选用sz命令,但是一执行这个命令,终端就会被卡着。解决办法:换成使用xshell连接远程服务器,或者其他支持lrzsz上传服务的软件。lrzsz:本地和服务器的文件互传工具下载安装方式:yum -y install lrzsz当然也可以使用wget方式,只不过有点麻烦。命令解析:rz (receive)从本地上传到远程服务sz (send)从远程服务到本地..._putty rz上传文件没反应

Android7.0 IMS(1)开机初始化_android监听imsmanager初始化状态-程序员宅基地

文章浏览阅读9.2k次。IMS(IP Multimedia Subsystem)被认为是下一代网络的核心技术,是解决移动与固网融合,引入语音、数据、视频三重融合等差异化业务的重要方式。Android作为移动网络终端的主要操作系统,也提供了对IMS的支持。 本篇博客的目的就是弄清楚Android中的IMS是如何完成开机初始化的。_android监听imsmanager初始化状态

【DPDK】dpdk样例源码解析之四:dpdk-ip_reassembly_dpdk example的功能-程序员宅基地

文章浏览阅读1.7k次,点赞5次,收藏10次。dpdk IP重组_dpdk example的功能

随便推点

云原生|容器和应用安全运营实践思考-程序员宅基地

文章浏览阅读1.8k次。文| 腾讯“洋葱”入侵对抗团队bghost前言随着云计算的蓬勃发展,云原生概念被提出并快速发展,公司内部也在推进使用云原生技术进行架构优化,研发模式和基础设施都发生了很大的变化,新的k8s..._腾讯云部署容器安全 操作

exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException-程序员宅基地

文章浏览阅读2.1k次。spring_org.springframework.beans.factory.nouniquebeandefinitionexception

conda新建环境时报错NotWritableError: The current user does not have write permissions_environmentnotwritableerror: the current user does-程序员宅基地

文章浏览阅读4.9k次,点赞5次,收藏20次。目录1、问题描述2、问题原因3、解决方案4、测试5、参考自1、问题描述在使用conda create -n environment_name命令新建环境时,遇到错误:Solving environment: failedNotWritableError: The currenuser does not have write permissions to a required path. path: /home/user_name/.conda/pkgs/u.._environmentnotwritableerror: the current user does not have write permission

Zeppelin的Tableau可视化结果的优化小技巧(四)_tableau时间坐标轴顺序-程序员宅基地

文章浏览阅读171次。文章目录在折线图末尾添加自定义图形以增加美感制作方法追记在折线图末尾添加自定义图形以增加美感本期博客将继续为大家介绍一个Tableau的可视化结果的优化技巧,即在折线图的末尾处添加自定义图形用以增加整体美感。(注:此次演示只介绍了技巧,并未真正使用自定义图形,因此美感提升有限,望各位海涵)本次可视化所使用的数据依然来自开源的OECD Data。制作方法我们将以如下简单的折线图开始,完成自定义图形的追加。首先我们观察到折线图本身下方留白较多,因此推荐通过取消显示原点来更好地反应折线的起伏。_tableau时间坐标轴顺序

转发:5G NR MCG,SCG,PCell,PSCell,SCell,sPCell 概念澄清_scg去激活和scg释放-程序员宅基地

文章浏览阅读3.4k次。感谢光光学长的分享,原文请见https://blog.csdn.net/jxwxg/article/details/85225883_scg去激活和scg释放

求最大公约数和最小公倍数_第1关:求最大公约数-程序员宅基地

文章浏览阅读4.6k次,点赞12次,收藏19次。1. 求最大公约数求最大公约数的过程: 若a、b全为零则它们的最大公寓书不存在;若a、b其中之一为零,则它们的最大公约数为a、b中非零的那个;若a、b都不为零,则使新a=b;新b=a%b然后重复该过程。所谓求a,b的最大公约数,就是求同时满足a%c=0、b%c=0的最大正整数c,即求能够同时整除a和b的最大正整数c。非递归形式#include&quot;stdio.h&quot;int gcd(i..._第1关:求最大公约数

推荐文章

热门文章

相关标签