forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteemo-attacking.cpp
More file actions
29 lines (24 loc) · 748 Bytes
/
teemo-attacking.cpp
File metadata and controls
29 lines (24 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//https://leetcode.com/problems/teemo-attacking/
//Difficulty Level: Medium
//Tags: Arrays
//Time complexity: O(n)
//Space complexity: O(1)
//similar to overlapping subintervals problems
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
int n = timeSeries.size();
if(n==0)
return 0;
int ans = duration;
for(int i=0; i<n-1; i++)
{
ans += duration;
if(timeSeries[i+1] < timeSeries[i] + duration) //overlapping condition
{
ans -= (timeSeries[i] + duration - timeSeries[i+1]); //the overlapped time subtracted
}
}
return ans;
}
};