Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d86436e

Browse files
commit go 495
1 parent 7427571 commit d86436e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎go/495.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
/**
6+
* LC#495:提莫攻击
7+
* Link:https://leetcode-cn.com/problems/teemo-attacking/
8+
* 思路:考虑相邻两个攻击时间点 A[i] 和 A[i + 1] 以及中毒持续时间 t,如果 A[i] + t <= A[i + 1],那么在第 i + 1 次攻击时,第 i 次攻击造成的中毒的持续时间已经结束,即持续时间为 t;如果 A[i] + t > A[i + 1],那么在第 i + 1 次攻击时,第 i 次攻击的中毒仍然在持续,由于中毒状态不可叠加,因此持续时间为 A[i + 1] - A[i]
9+
*/
10+
func min(interval int, duration int) int {
11+
if interval < duration {
12+
return interval
13+
}
14+
return duration
15+
}
16+
17+
func findPoisonedDuration(timeSeries []int, duration int) int {
18+
total := 0
19+
for i := 0; i < len(timeSeries)-1; i++ {
20+
total += min(timeSeries[i+1]-timeSeries[i], duration)
21+
}
22+
return total + duration
23+
}
24+
25+
func main() {
26+
res := findPoisonedDuration([]int{1, 3, 5, 7, 9}, 3)
27+
fmt.Println("res >>>", res)
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /