LeetCode Solutions

495. Teemo Attacking

Time: $O(n)$

Space: $O(1)$

			

class Solution {
 public:
  int findPoisonedDuration(vector<int>& timeSeries, int duration) {
    if (duration == 0)
      return 0;

    int ans = 0;

    for (int i = 0; i + 1 < timeSeries.size(); ++i)
      ans += min(timeSeries[i + 1] - timeSeries[i], duration);

    return ans + duration;
  }
};
			

class Solution {
  public int findPoisonedDuration(int[] timeSeries, int duration) {
    if (duration == 0)
      return 0;

    int ans = 0;

    for (int i = 0; i + 1 < timeSeries.length; ++i)
      ans += Math.min(timeSeries[i + 1] - timeSeries[i], duration);

    return ans + duration;
  }
}
			

class Solution:
  def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
    if duration == 0:
      return 0

    ans = 0

    for i in range(0, len(timeSeries) - 1):
      ans += min(timeSeries[i + 1] - timeSeries[i], duration)

    return ans + duration