Skip to content

Commit 3024684

Browse files
authored
Merge pull request #187 from gostool/leetcode0495
Leetcode0495
2 parents 79a5d9e + a2859d0 commit 3024684

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func findPoisonedDuration(timeSeries []int, duration int) int {
4+
var ans int
5+
for i := 1; i < len(timeSeries); i++ {
6+
t := timeSeries[i-1]
7+
end := t + duration - 1
8+
if end < timeSeries[i] {
9+
ans += duration
10+
} else {
11+
ans += timeSeries[i] - t
12+
}
13+
}
14+
ans += duration
15+
return ans
16+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question495 struct {
9+
para495
10+
ans495
11+
}
12+
13+
// para 是参数
14+
type para495 struct {
15+
timeSeries []int
16+
duration int
17+
}
18+
19+
// ans 是答案
20+
type ans495 struct {
21+
ans int
22+
}
23+
24+
func Test_Problem495(t *testing.T) {
25+
26+
qs := []question495{
27+
28+
{
29+
para495{[]int{1, 4}, 2},
30+
ans495{4},
31+
},
32+
33+
{
34+
para495{[]int{1, 2}, 2},
35+
ans495{3},
36+
},
37+
}
38+
39+
fmt.Printf("------------------------Leetcode Problem 495------------------------\n")
40+
41+
for _, q := range qs {
42+
_, p := q.ans495, q.para495
43+
fmt.Printf("【input】:%v 【output】:%v\n", p, findPoisonedDuration(p.timeSeries, p.duration))
44+
}
45+
fmt.Printf("\n\n\n")
46+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [495. Teemo Attacking](https://leetcode-cn.com/problems/teemo-attacking/)
2+
3+
4+
## 题目
5+
6+
Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds.
7+
8+
More formally, an attack at second t will mean Ashe is poisoned during the inclusive time interval [t, t + duration - 1].
9+
10+
If Teemo attacks again before the poison effect ends, the timer for it is reset, and the poison effect will end duration seconds after the new attack.
11+
12+
You are given a non-decreasing integer array timeSeries, where timeSeries[i] denotes that Teemo attacks Ashe at second timeSeries[i], and an integer duration.
13+
14+
Return the total number of seconds that Ashe is poisoned.
15+
16+
**Example 1**:
17+
```
18+
Input: timeSeries = [1,4], duration = 2
19+
Output: 4
20+
Explanation: Teemo's attacks on Ashe go as follows:
21+
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
22+
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
23+
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
24+
```
25+
26+
**Example 2**:
27+
```
28+
Input: timeSeries = [1,2], duration = 2
29+
Output: 3
30+
Explanation: Teemo's attacks on Ashe go as follows:
31+
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
32+
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
33+
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.
34+
```
35+
36+
**Constraints**:
37+
38+
- 1 <= timeSeries.length <= 10000
39+
- 0 <= timeSeries[i], duration <= 10000000
40+
- timeSeries is sorted in non-decreasing order.
41+
42+
## 题目大意
43+
44+
在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄。他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。
45+
46+
当提莫攻击艾希,艾希的中毒状态正好持续duration 秒。
47+
48+
正式地讲,提莫在t发起发起攻击意味着艾希在时间区间 [t, t + duration - 1](含 t 和 t + duration - 1)处于中毒状态。
49+
50+
如果提莫在中毒影响结束前再次攻击,中毒状态计时器将会重置,在新的攻击之后,中毒影响将会在duration秒后结束。
51+
52+
给你一个非递减的整数数组timeSeries,其中timeSeries[i]表示提莫在timeSeries[i]秒时对艾希发起攻击,以及一个表示中毒持续时间的整数duration 。
53+
54+
返回艾希处于中毒状态的总秒数。
55+
56+
## 解题思路
57+
58+
- i从1开始计数,令t等于timeSeries[i - 1]
59+
- 比较end(t + duration - 1)和timeSeries[i]的大小,
60+
- 如果end小于timeSeries[i],ans+=duration
61+
- 否则ans += timeSeries[i] - t
62+
- ans += duration并返回ans
63+
64+
## 代码
65+
66+
```go
67+
package leetcode
68+
69+
func findPoisonedDuration(timeSeries []int, duration int) int {
70+
var ans int
71+
for i := 1; i < len(timeSeries); i++ {
72+
t := timeSeries[i-1]
73+
end := t + duration - 1
74+
if end < timeSeries[i] {
75+
ans += duration
76+
} else {
77+
ans += timeSeries[i] - t
78+
}
79+
}
80+
ans += duration
81+
return ans
82+
}
83+
```

0 commit comments

Comments
 (0)