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 attackingascendingtime 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.
Brainstorm
The story adds a bit of complication to the actual problem, but we can start dissecting by taking a look at the examples first. We see that there are 2 ways the total duration of poison can be changed:
- Teemo's poison duration gets added to the total duration (as seen in Example 1)
- Teemo's poison duration is not fully added, but is "reset" by the next duration (as seen in Example 2).
It seems that (1) is the more general case. However, when does (2) occur? If we take a look at Example 2, we see that when Teemo poisons Ashe at time point 1, the poison should last for 2 seconds until time point 3, but Teemo poisons Ashe again at time point 2 and "resets" the first poison's duration to 1 second. In other words, the poison's duration from time point 1 didn't last long enough to heal before the next poison: timePoint1 + duration > timePoint2. In this situation, the poison only lasts as long as the time between the two time points, i.e.timePoint2 - timePoint1.
Otherwise, the poison will last as long as the given duration. So now we know how to add to the total duration of poison under these two situations.
Approach #1: One Loop, Add Up Durations
We iterate through the array and add a different value to totalDurationdepending on the two conditions we discussed previously. Due to the nature of the loop, we can only go up to the last element with our calculations. What happens at the last time point? I was a bit hesitant here initially, as I wasn't sure whether duration will always be added to totalDuration. However, I realized that the poison that Teemo shoots here will last as long as the given duration, because this _is _the last time point, and there won't be any later time point that "resets" duration! This completes our algorithm.
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
int totalDuration = 0, n = timeSeries.size();
if (n == 0)
return totalDuration;
for (int i = 0; i < n - 1; i++) {
if (timeSeries[i] + duration > timeSeries[i + 1])
totalDuration += timeSeries[i+1] - timeSeries[i];
else
totalDuration += duration;
}
return totalDuration + duration;
}
Time complexity: $$O(n)$$
We calculate everything in one loop.
Space complexity: $$O(1)$$