Computer Science/알고리즘
[Leetcode] Teemo Attacking
GOD동하
2020. 9. 28. 13:16
leetcode.com/problems/teemo-attacking/
Teemo Attacking - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Medium 이지만 Easy 급의 문제였던 난이도.
Poisoning 되는 Duration이 안겹치도록만 total 값을 꺼내주면 됨
현재값, 다음값 차이보다 duration보다 크면 겹치지 않는다는거니까 독에 걸린 시간은 딱 duration 만큼.
현재값, 다음값 차이가 duration보다 크면 겹치는거니까 딱 겹치기 전까지의 duration 만큼만 더해주면 됨
쉬움 끗
public static int findPoisonedDuration(int[] timeSeries, int duration) {
if (timeSeries.length == 0) return 0;
int total = 0;
for (int i = 0; i < timeSeries.length - 1; i++) {
int now = timeSeries[i];
int next = timeSeries[i+1];
total += Math.min(next - now, duration);
}
return total + duration;
}