티스토리 뷰

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;
    }

'Computer Science > 알고리즘' 카테고리의 다른 글

[Leetcode] Largest Number  (2) 2020.09.28
[Leetcode] Find the Difference  (0) 2020.09.25
[Leetcode] Car Pooling  (1) 2020.09.22
[Leetcode] Multiply Strings  (0) 2020.09.15
[정올] 순서찾기 (문제번호 : 2437)  (0) 2020.02.17
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함