leetcode 495. Teemo Attacking

来源:互联网 发布:mac 视频特效 编辑:程序博客网 时间:2024/06/11 05:08

1.题目

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

英雄Teemo对敌人 Ashe 使用毒药会使敌人中毒一段时间。
给出一个时间序列timeSeries表示英雄对敌人使用毒药的时间点,duration表示毒药的持续时间。求敌人总共有多长时间保持中毒状态。
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
4-1>2 所以 2+2=4
Example 2:
Input: [1,2], 2
Output: 3
(2-1)+2=3

2.分析

判断两个使用毒药的时间间隔delta
delta>= duration, 持续时间+=duration
delta< duration, 持续时间+=delta

3.代码

class Solution {public:    int findPoisonedDuration(vector<int>& timeSeries, int duration) {        if(timeSeries.size()<1)            return 0;        int count = duration;        for (int i = 1; i < timeSeries.size(); i++)            count += timeSeries[i] - timeSeries[i - 1] >= duration ? duration : timeSeries[i] - timeSeries[i - 1];        return count;    }};