495: Solution with step by step explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Begin by defining a function findPoisonedDuration that takes in two ar
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Begin by defining a function findPoisonedDuration that takes in two arguments - a list timeSeries containing non-decreasing integers and an integer duration.\n\n2. Check if the list timeSeries is empty. If it is, return 0 as there are no poison attacks.\n\n3. Get the length of the list timeSeries and initialize a variable poisoned_time to 0.\n\n4. Iterate through the list timeSeries from index 0 to n-2, where n is the length of timeSeries.\n\n5. At each iteration, calculate the duration of the poison effect by finding the minimum value between duration and the difference between the next and current element in the list.\n\n6. Add the calculated duration of the poison effect to the poisoned_time variable.\n\n7. Once the loop is completed, add the duration of the last poison effect to the poisoned_time variable.\n\n8. Return the value of poisoned_time.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n  def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:\n    if not timeSeries:\n        return 0\n    n = len(timeSeries)\n    poisoned_time = 0\n    \n    for i in range(n-1):\n        # calculate the duration of the poison effect\n        duration_of_effect = min(duration, timeSeries[i+1]-timeSeries[i])\n        poisoned_time += duration_of_effect\n        \n    # add the duration of the last poison effect\n    poisoned_time += duration\n    \n    return poisoned_time\n\n```
['Array', 'Simulation', 'Python', 'Python3']