Skip to content

Commit a531758

Browse files
committed
495. Teemo Attacking
1 parent 55ab10b commit a531758

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ The project is divided into two parts: `structure` and `solution`.
165165
| 386 | [Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) | Medium | [LexicographicalNumbers.java](src/leetcode/solution/array/LexicographicalNumbers.java) | Recursion |
166166
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | Easy | [FizzBuzz.java](src/leetcode/solution/array/FizzBuzz.java) | For loop. Calculate i%5 and i%3 separately. |
167167
| 456 | [132 Pattern](https://leetcode.com/problems/132-pattern/) | Medium | [OneThreeTwoPattern.java](src/leetcode/solution/array/OneThreeTwoPattern.java) | Using Array as a Stack |
168+
| 495 | [Teemo Attacking](https://leetcode.com/problems/teemo-attacking/) | Easy | [TeemoAttacking.java](src/leetcode/solution/array/TeemoAttacking.java) | Simulation |
168169
| 768 | [Max Chunks To Make Sorted II](https://leetcode.com/problems/max-chunks-to-make-sorted-ii/) | Hard | [MaxChunksToMakeSortedII.java](src/leetcode/solution/array/MaxChunksToMakeSortedII.java) | Iterate through the array, each time all elements to the left are smaller (or equal) to all elements to the right, there is a new chunk. |
169170
| 769 | [Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/) | Medium | [MaxChunksToMakeSorted.java](src/leetcode/solution/array/MaxChunksToMakeSortedII.java) | Iterate through the array, each time the maximum value of all elements to the left equals the index, there is a new chunk. |
170171
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/) | Easy | [VerifyingAnAlienDictionary.java](src/leetcode/solution/array/VerifyingAnAlienDictionary.java) | Compare adjacent elements |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetcode.solution.array;
2+
3+
/**
4+
* 495. Teemo Attacking
5+
*/
6+
public class TeemoAttacking {
7+
8+
public static void main(String[] args) {
9+
int duration = 2;
10+
int[] timeSeries = {1, 4};
11+
TeemoAttacking teemoAttacking = new TeemoAttacking();
12+
System.out.println(teemoAttacking.findPoisonedDuration(timeSeries, duration));
13+
}
14+
15+
public int findPoisonedDuration(int[] timeSeries, int duration) {
16+
if (timeSeries.length == 0) {
17+
return 0;
18+
}
19+
20+
int total = 0;
21+
int end = timeSeries[0];
22+
23+
for (int i = 0; i < timeSeries.length; i++) {
24+
if (end > duration + timeSeries[i]) {
25+
continue;
26+
} else if (timeSeries[i] < end) {
27+
total += timeSeries[i] + duration - end;
28+
end = timeSeries[i] + duration;
29+
} else {
30+
total += duration;
31+
end = timeSeries[i] + duration;
32+
}
33+
}
34+
35+
return total;
36+
37+
}
38+
}

0 commit comments

Comments
 (0)