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.java
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:code
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.ci
Example 2:get
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.it
Note:io
LOL裏面有一個英雄名叫Teemo,它的一個技能是在敵人區域釋放毒藥而且可以持續一段時間。如今傳入一個數組,分別表示Teemo釋放技能的時間點,以及一個整數表示技能持續的時間,問敵人一共被毒多長時間?要注意,若是在釋放技能期間重複釋放技能,技能時間是從當前時間開始從新計算的。ast
簡單來講,該技能釋放的時間點一共有兩種狀況:class
若是無毒的話,只須要將技能持續時間累加到總時間上便可。而若是有毒的話,就須要計算額外延長的毒藥時間,經過 當前時間+技能持續時間-上一個技能持續時間 得出。代碼以下:im
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; }