In a city, a streetlight turns on automatically and stays on for exactly B seconds each time it detects motion. If it detects motion again before turning off, the timer resets, and it stays on for another B seconds from the new detection time.
You have a list A where each element A[i] indicates the time when the streetlight detects motion, and an integer B. Calculate the total time the streetlight remains on.
Return an Integer, denoting the total time the streetlight remains on.
Input 1: A = [1, 4] B = 2
Input 2: A = [1, 2] B = 2
Output 1: 4
Output 2: 3
Explanation 1: At second 1, the streetlight detects motion and turns on for seconds 1 and 2. At second 4, the streetlight detects motion again and turns on for seconds 4 and 5. The streetlight is on for seconds 1, 2, 4, and 5, which is 4 seconds in total.
Explanation 2: At second 1, the streetlight detects motion and turns on for seconds 1 and 2. At second 2, the streetlight detects motion again and resets the timer. It stays on for seconds 2 and 3. The streetlight is on for seconds 1, 2, and 3, which is 3 seconds in total.
Hackerrank • Pending