Skip to content

Docs: Added Solutions to Leetcode 495 #1230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dsa-problems/leetcode-problems/0400-0499.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ export const problems = [
"problemName": "495. Teemo Attacking",
"difficulty": "Medium",
"leetCodeLink": "https://leetcode.com/problems/teemo-attacking",
"solutionLink": "#"
"solutionLink": "/dsa-solutions/lc-solutions/0400-0499/teemo-attacking"
},
{
"problemName": "496. Next Greater Element I",
Expand Down
140 changes: 140 additions & 0 deletions dsa-solutions/lc-solutions/0400-0499/0495-teemo-attacking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
---
id: teemo-attacking
title: Teemo Attacking
sidebar_label: 0495 - Teemo Attacking
tags:
- Greedy
- Array
- Simulation
description: "This is a solution to the Teemo Attacking problem on LeetCode."
---

## Problem Description

Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly `duration` seconds. More formally, an attack at second `t` will mean Ashe is poisoned during the **inclusive** time interval `[t, t + duration - 1]`. 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.

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`.

Return the **total** number of seconds that Ashe is poisoned.

### Examples

**Example 1:**
```
Input: timeSeries = [1,4], duration = 2
Output: 4
Explanation: Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
```

**Example 2:**

```
Input: timeSeries = [1,2], duration = 2
Output: 3
Explanation: Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.
```

### Constraints

- $1 <= timeSeries.length <= 10^4$
- $0 <= timeSeries[i], duration <= 10^7$
- `timeSeries` is sorted in **non-decreasing** order.

## Solution for Teemo Attacking

### Approach 1: One pass
#### Intuition

The problem is an example of merge interval questions which are now quite popular in Google.

Typically such problems could be solved in a linear time in the case of sorted input, and in O(Nlog⁡N) time otherwise.

Here one deals with a sorted input, and the problem could be solved in one pass with a constant space. The idea is straightforward: consider only the interval between two attacks. Ashe spends in a poisoned condition the whole time interval if this interval is shorter than the poisoning time duration `duration`, and `duration` otherwise.

#### Algorithm

- Initiate total time in poisoned condition `total = 0`.

- Iterate over `timeSeries` list. At each step add to the total time the minimum between interval length and the poisoning time duration `duration`.

- Return `total + duration` to take the last attack into account.

![image](https://assets.leetcode.com/static_assets/media/original_images/495/ashe.png)
## Code in Different Languages

<Tabs>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@Shreyash3087"/>

```cpp
class Solution {
public:
int findPoisonedDuration(std::vector<int>& timeSeries, int duration) {
int n = timeSeries.size();
if (n == 0) return 0;

int total = 0;
for(int i = 0; i < n - 1; ++i)
total += std::min(timeSeries[i + 1] - timeSeries[i], duration);
return total + duration;
}
};
```
</TabItem>
<TabItem value="java" label="Java">
<SolutionAuthor name="@Shreyash3087"/>

```java
class Solution {
public int findPoisonedDuration(int[] timeSeries, int duration) {
int n = timeSeries.length;
if (n == 0) return 0;

int total = 0;
for(int i = 0; i < n - 1; ++i)
total += Math.min(timeSeries[i + 1] - timeSeries[i], duration);
return total + duration;
}
}
```

</TabItem>
<TabItem value="python" label="Python">
<SolutionAuthor name="@Shreyash3087"/>

```python
class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
n = len(timeSeries)
if n == 0:
return 0

total = 0
for i in range(n - 1):
total += min(timeSeries[i + 1] - timeSeries[i], duration)
return total + duration
```
</TabItem>
</Tabs>

## Complexity Analysis

### Time Complexity: $O(N)$

> **Reason**: where N is the length of the input list since we iterate the entire list.

### Space Complexity: $O(1)$

> **Reason**: it's a constant space solution.

## References

- **LeetCode Problem**: [Teemo Attacking](https://leetcode.com/problems/teemo-attacking/description/)

- **Solution Link**: [Teemo Attacking](https://leetcode.com/problems/teemo-attacking/solutions/)
Loading