Skip to content

Commit 5459b7b

Browse files
committed
Add solution #495
1 parent d3b9fc5 commit 5459b7b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@
396396
492|[Construct the Rectangle](./0492-construct-the-rectangle.js)|Easy|
397397
493|[Reverse Pairs](./0493-reverse-pairs.js)|Hard|
398398
494|[Target Sum](./0494-target-sum.js)|Medium|
399+
495|[Teemo Attacking](./0495-teemo-attacking.js)|Easy|
399400
496|[Next Greater Element I](./0496-next-greater-element-i.js)|Easy|
400401
500|[Keyboard Row](./0500-keyboard-row.js)|Easy|
401402
501|[Find Mode in Binary Search Tree](./0501-find-mode-in-binary-search-tree.js)|Easy|

solutions/0495-teemo-attacking.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 495. Teemo Attacking
3+
* https://leetcode.com/problems/teemo-attacking/
4+
* Difficulty: Easy
5+
*
6+
* Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets
7+
* poisoned for a exactly duration seconds. More formally, an attack at second t will mean Ashe is
8+
* poisoned during the inclusive time interval [t, t + duration - 1]. If Teemo attacks again before
9+
* the poison effect ends, the timer for it is reset, and the poison effect will end duration
10+
* seconds after the new attack.
11+
*
12+
* You are given a non-decreasing integer array timeSeries, where timeSeries[i] denotes that Teemo
13+
* attacks Ashe at second timeSeries[i], and an integer duration.
14+
*
15+
* Return the total number of seconds that Ashe is poisoned.
16+
*/
17+
18+
/**
19+
* @param {number[]} timeSeries
20+
* @param {number} duration
21+
* @return {number}
22+
*/
23+
var findPoisonedDuration = function(timeSeries, duration) {
24+
return timeSeries.reduce((sum, n, i) => {
25+
return sum + Math.min(duration, (timeSeries[i + 1] ?? n + duration) - n);
26+
}, 0);
27+
};

0 commit comments

Comments
 (0)