Similar Problems
Similar Problems not available
Teemo Attacking - Leetcode Solution
Companies:
LeetCode: Teemo Attacking Leetcode Solution
Difficulty: Easy
Topics: array simulation
Problem Statement:
Teemo is attacking an enemy champion that has a total of n health points. His attack deals damage per second = DPS and attacks per second = APS. In each second, Teemo can deal DPS amount of damage and/or apply an extra DOT (damage-over-time) of the same DPS to the target for next second, which can be repeated indefinitely until the target dies. If Teemo chooses to apply the DOT, it will immediately begin dealing damage on the subsequent second.
Return the minimum number of seconds that Teemo needs to defeat the enemy champion.
Example 1:
Input: healthPoints = 13, DPS = 5, APS = 3 Output: 5 Explanation: Teemo can deal 5 damage per second. The enemy champion's health is reduced to 8 in the first second ([3] attacks + [2] dots = 5 damage). The enemy champion's health is reduced to 3 in the second second ([3] attacks + [2] dots = 5 damage). The enemy champion's health is reduced to -2 in the third second ([3] attacks + [2] dots = 5 damage). Teemo has dealt a total of 13 damage to the enemy champion, which is enough to defeat it. Therefore, the answer is 3 + 1 = 4 seconds. Example 2:
Input: healthPoints = 14, DPS = 3, APS = 2 Output: 7 Explanation: Teemo can deal 3 damage per second. The enemy champion's health is reduced to 11 in the first second ([2] attacks + [1] dot = 3 damage). The enemy champion's health is reduced to 8 in the second second ([2] attacks + [1] dot = 3 damage). The enemy champion's health is reduced to 5 in the third second ([2] attacks + [1] dot = 3 damage). The enemy champion's health is reduced to 2 in the fourth second ([2] attacks + [1] dot = 3 damage). The enemy champion's health is reduced to -1 in the fifth second ([2] attacks + [1] dot = 3 damage). Teemo has dealt a total of 14 damage to the enemy champion, which is enough to defeat it. Therefore, the answer is 5 + 1 = 6 seconds.
Solution
The problem mentions that Teemo can either attack or use a damage-over-time effect on the target. So, we can calculate the total damage that Teemo can deal in each second and check whether using the DOT effect is beneficial or not.
Let's say x is the number of seconds required to defeat the enemy champion without using the DOT effect. In this case, Teemo will simply attack the enemy champion x times with his normal attacks and defeat the champion.
On the other hand, if Teemo uses the DOT effect, then he can deal DPS amount of damage in the first second and DPS amount of damage on the second second, and so on until the target is dead. In this case, if we assume y is the number of seconds that Teemo needs to use the DOT effect to defeat the champion, then he will be able to deal DPS * y amount of damage to the target.
So, the condition for using the DOT effect will be:
DPS * y > DPS * x
which can be simplified to:
y > x
Therefore, if using the DOT effect can defeat the enemy champion faster than directly attacking him, then we should choose to use the DOT effect.
Let's take an example to understand the solution better:
healthpoints = 13, DPS = 5, APS = 3
Here, Teemo can attack the target 3 times per second and deals 5 damage with each attack. Therefore, he can deal 15 damage in 1 second if he attacks all 3 times. Alternatively, he can use the DOT effect and deal 5 damage in the first second, and in the second second, he can deal 5 damage from the previous DOT plus 5 damage from the new DOT, which is a total of 10 damage in 2 seconds.
So, it can be seen that using the DOT effect is beneficial in this case. Therefore, we will calculate the number of seconds required to defeat the champion using the DOT effect.
The damage dealt in each second will be:
first second: 5 (DOT effect)
second second: 5 + 5 (DOT effect)
third second: 5 + 5 + 5 (DOT effect and attack)
fourth second: 5 + 5 + 5 + 5 (DOT effect and attack)
fifth second: 5 + 5 + 5 + 5 + 5 (DOT effect and attack)
So, the total damage dealt will be 25, which is enough to defeat the champion. Therefore, Teemo needs 5 seconds to defeat the champion using the DOT effect.
Conversely, if we consider the case where we only attack the target without using the DOT effect, then Teemo will need 3 seconds to deal 15 damage to the target. Therefore, using the DOT effect is faster and better in this scenario.
To implement this logic in code, we can start by calculating the number of seconds required to defeat the champion using normal attacks:
int x = (healthPoints + APS - 1) / APS;
Then, we can calculate the number of seconds required to defeat the champion using the DOT effect:
int y = (DPS * x + healthPoints - 1) / healthPoints;
Finally, we can return the minimum of these two values:
return min(x, y) + (y > x);
The last line is used to add an extra second if we use the DOT effect because the DOT effect starts dealing damage from the second second.
Time Complexity:
The time complexity of this solution is O(1) as we are calculating the result through basic arithmetic operations and there are no loops or recursive functions involved.
Space Complexity:
The space complexity of this solution is O(1) as we are not creating any additional data structures or using any recursion. We are only storing a few variables to keep track of different values.
Teemo Attacking Solution Code
1