Python 3 Easy Solution Explained
\nclass Solution:\n    def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:\n        if duration == 0: return 0\n        res = 0\n      
```\nclass Solution:\n    def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:\n        if duration == 0: return 0\n        res = 0\n        for i in range(len(timeSeries)-1):\n            res += min(timeSeries[i] + duration, timeSeries[i+1]) - timeSeries[i]\n        res += duration\n        return res\n```\n\nSo, let us break the above solution. \n\nFirstly, in the constraints there is a possibility that **duration** can be 0 which means no matter how much Teemo attacks, Ashe will not be poisoned for even a second. So this is treated as an edge case *(although our code could have dealt with the duration 0 thing even without the need of putting an edge case condition return, but this was my first instinct so I went with this)*.\n\nNow, what we want is to count the seconds our enemy (Ashe) is poisoned. According to our problem statement, if Teemo attacks Ashe when they are already poisoned, the duration will reset and continue from that second. Which is another way to state that the time frames of Ashe being poisoned can and will overlap. \n\nExplaining what I mean is let us take an example. So for the below conditions. \n```\nduration = 5, timeSeries  = [1,3]\n```\nTeemo first attacks at **Second 1** and Ashe is supposed to be poisoned for **Second 1, Second 2, Second 3, Second 4 and Second 5**. \n\nBut, Teemo could not wait and attacked at **Second 3** again which restarted the timer again and now, Ashe will also be poisoned for **Second 3, Second 4, Second 5, Second 6, Second 7**.\n\nThat gives us a total of **7 Seconds** of Ashe being poisoned for the above variables as **Second 3, Second 4, Second 5** are common and are overlapping and will basically be counted only once. \n\nSo to simulate the counting of the poisoned seconds, my approach is to start a loop for each time Teemo attacked except the last time (basically a loop from 1st element to the second-last element), and add the duration minus the overlapped seconds. \n\nTo check/calculate how may seconds are relevant from each attack from Teemo, just check if the last poisoned second of the attack goes past the next attack\'s time. If it does, we do not count any time that was past the next attack\'s time. Those seconds will be counted in the next iteration of the loop. \n\ne.g. To explain further, take the above example. \n```\ntimeSeries = [1,3]. duration = 5\n```\nSo for the first attack, \n**Poisoned Time - 1 2 3 4 5 = 5 seconds**\n\nBut, there is a second attack om **Second 3** before Ashe is recovered. So from the first attack, 3 4 5 were irrelevant for our count and Teemo only managed to effectively poison Aishe for only **2 seconds** before they landed their next attack.\n\nIn our code, we calculate that using the minimum function to see which is minimum; The next attack\'s time (in case of an overlap) or the time when Ashe recovers (in case where there is no overlap with the next attack) and subtract our original time of attack to calculate the duration Ashe was effectively poisoned for that attack. \n\nWe do this for all attacks but the last one in our list and deal with it separately outside our for loop because there is going to be no next attack for which we need to check any overlapping and we can directly just add the **duration** variable in our result and return it.\n\n**Note:** We can only implement the above solution when our **timeSeries** variable is sorted (which it is), or generally we would had to separately sort it before going forward or we would have not been able to directly compare one attack with the other as they were basically not in a sequential order. \n\n**Author Notes:** Sorry this is a very long solution to read, but feel free to ask if anything is unclear or you need clarity on what I explained. \n\n**Happy Coding!!**\n\n\n
['Python', 'Python3']