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 82039c3

Browse files
✨feat: Add 495
1 parent 50e95c8 commit 82039c3

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
| [457. 环形数组是否存在循环](https://leetcode-cn.com/problems/circular-array-loop/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/circular-array-loop/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-ag05/) | 中等 | 🤩🤩🤩🤩 |
4444
| [482. 密钥格式化](https://leetcode-cn.com/problems/license-key-formatting/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/license-key-formatting/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-piya/) | 简单 | 🤩🤩🤩🤩 |
4545
| [492. 构造矩形](https://leetcode-cn.com/problems/construct-the-rectangle/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/construct-the-rectangle/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-7ser/) | 简单 | 🤩🤩🤩🤩 |
46+
| [495. 提莫攻击](https://leetcode-cn.com/problems/teemo-attacking/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/teemo-attacking/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-gteh/) | 简单 | 🤩🤩🤩🤩🤩 |
4647
| [500. 键盘行](https://leetcode-cn.com/problems/keyboard-row/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/keyboard-row/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-zx6b/) | 简单 | 🤩🤩🤩🤩 |
4748
| [528. 按权重随机选择](https://leetcode-cn.com/problems/random-pick-with-weight/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-pick-with-weight/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-8bx50/) | 中等 | 🤩🤩🤩🤩 |
4849
| [541. 反转字符串 II](https://leetcode-cn.com/problems/reverse-string-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-string-ii/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-p88f/) | 简单 | 🤩🤩🤩🤩🤩 |
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[495. 提莫攻击](https://leetcode-cn.com/problems/teemo-attacking/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-gteh/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
10+
在《英雄联盟》的世界中,有一个叫 "提莫" 的英雄。他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。
11+
12+
当提莫攻击艾希,艾希的中毒状态正好持续 `duration` 秒。
13+
14+
正式地讲,提莫在 `t` 发起发起攻击意味着艾希在时间区间 `[t, t + duration - 1]`(含 `t``t + duration - 1`)处于中毒状态。如果提莫在中毒影响结束 前 再次攻击,中毒状态计时器将会 重置 ,在新的攻击之后,中毒影响将会在 `duration` 秒后结束。
15+
16+
给你一个 非递减 的整数数组 `timeSeries`,其中 `timeSeries[i]` 表示提莫在 `timeSeries[i]` 秒时对艾希发起攻击,以及一个表示中毒持续时间的整数 `duration`
17+
18+
返回艾希处于中毒状态的 **** 秒数。
19+
20+
示例 1:
21+
```
22+
输入:timeSeries = [1,4], duration = 2
23+
24+
输出:4
25+
26+
解释:提莫攻击对艾希的影响如下:
27+
- 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
28+
- 第 4 秒,提莫再次攻击艾希,艾希中毒状态又持续 2 秒,即第 4 秒和第 5 秒。
29+
艾希在第 1、2、4、5 秒处于中毒状态,所以总中毒秒数是 4 。
30+
```
31+
示例 2:
32+
```
33+
输入:timeSeries = [1,2], duration = 2
34+
35+
输出:3
36+
37+
解释:提莫攻击对艾希的影响如下:
38+
- 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
39+
- 第 2 秒,提莫再次攻击艾希,并重置中毒计时器,艾希中毒状态需要持续 2 秒,即第 2 秒和第 3 秒。
40+
艾希在第 1、2、3 秒处于中毒状态,所以总中毒秒数是 3 。
41+
```
42+
43+
提示:
44+
* 1ドル <= timeSeries.length <= 10^4$
45+
* 0ドル <= timeSeries[i], duration <= 10^7$
46+
* timeSeries 按 非递减 顺序排列
47+
48+
---
49+
50+
### 模拟
51+
52+
题目已确保 $timeSeries$ 为非递减排序,按照顺序进行遍历处理即可。
53+
54+
我们使用 $ans$ 统计答案,使用 $last$ 记录上一次攻击的结束点,对于任意的 $timeSeries[i]$ 而言,假设其发起点为 $s = timeSeries[i],ドル结束点为 $e = s + duration - 1,ドル针对 $last$ 和 $s$ 进行分情况讨论即可:
55+
56+
* $last < s$ :两次攻击不重合,则有 $ans += duration; last = e;$
57+
* $last >= s$ :两次攻击重合,则有 $ans += e - last; last = e$
58+
59+
>注意:$last$ 作为上次的结束点,在处理 $timeSeries[i]$ 时,$last$ 是一个「已被统计」的存在,因此我们需要将其初始化为 $-1$(使用一个比 0ドル$ 小的数值作为哨兵),以确保当 $timeSeries[0] = 0$ 时,第 0ドル$ 秒能够被计数。
60+
61+
代码:
62+
```Java
63+
class Solution {
64+
public int findPoisonedDuration(int[] timeSeries, int duration) {
65+
int ans = 0, last = -1;
66+
for (int s : timeSeries) {
67+
int e = s + duration - 1;
68+
ans += last < s ? duration : e - last;
69+
last = e;
70+
}
71+
return ans;
72+
}
73+
}
74+
```
75+
* 时间复杂度:$O(n)$
76+
* 空间复杂度:$O(1)$
77+
78+
---
79+
80+
### 最后
81+
82+
这是我们「刷穿 LeetCode」系列文章的第 `No.495` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
83+
84+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
85+
86+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
87+
88+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
89+

0 commit comments

Comments
(0)

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