Topic describes
In the world of League of Legends, there is a hero named timo. His attacks can put the enemy hero Ash into a poisoned state.
When Timo attacks Ash, Ash is poisoned for duration seconds.
Formally, timo’s attack at T means that Ash is poisoned during the time interval [t, T + duration-1] (inclusive of t and T + duration-1). If Timo attacks again before the poison effect ends, the poison timer will reset, and after the new attack, the poison effect will end after Duration seconds.
You are given a non-decrement array of integer timeSeries, where timeSeries[I] indicates that Timo attacked Aish in timeSeries[I] seconds, and an integer duration that indicates the poisoning duration.
Returns the total number of seconds ash was poisoned.
The original problem
Answer key analysis
In this case, determine whether the current attack is poisoning. To determine the date of the last poisoning.
Reference code
public int findPoisonedDuration(int[] timeSeries, int duration) { int result = 0; For (int I = 0; i < timeSeries.length - 1; Int item = timeSeries[I + 1] -timeseries [I]; int item = timeSeries[I + 1]; If (item <= duration) {result = result + item; if (item <= duration) {result = result + item; } else {// If the time difference is greater than the duration of the poisoning, then the poisoning will last the entire time. result = result + duration; Result = result + timeSeries[timeSeries. Length-1]; return result; }Copy the code