public int findPoisonedDuration(int[] timeSeries, int duration) {
int length = 0;
for(int i=0;i<timeSeries.length;i++)
{
if(i==0timeSeries[i1]+duration<=timeSeries[i])
{
length+=duration;
}
else
{
length+=(timeSeries[i]timeSeries[i1]);
}
}
return length;
}
}
Simple O(n) Java Solution

Refactored a little for less lines
public class Solution { public int findPoisonedDuration(int[] timeSeries, int duration) { int length = 0; for(int i=0;i<timeSeries.length;i++) { length+=(i==0timeSeries[i1]+duration<=timeSeries[i])?duration:(timeSeries[i]timeSeries[i1]); } return length; } } ``