๐Ÿง  Teemo Attacking

๋ฌธ์ œ

In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemoโ€™s attacking ascending time series towards Ashe and the poisoning time duration per Teemoโ€™s attacking, you need to output the total time that Ashe is in poisoned condition. You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.

  • You may assume the length of given time series array wonโ€™t exceed 10000.
  • You may assume the numbers in the Teemoโ€™s attacking time series and his poisoning time duration per attacking are non-negative integers, which wonโ€™t exceed 10,000,000.

๋‚˜์˜ ํ’€์ด

def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
    poisoned = 0
    for idx, ts in enumerate(timeSeries):
        if idx == len(timeSeries) - 1 or ts + duration - 1 < timeSeries[idx+1]:
            poisoned += duration
        else:
            poisoned += timeSeries[idx+1] - ts
    return poisoned

ํ˜„์žฌ ์ธ๋ฑ์Šค์˜ ๊ฐ’๊ณผ ๋‹ค์Œ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ํ˜„์žฌ ์ธ๋ฑ์Šค + duration - 1์ด ๋‹ค์Œ ์ธ๋ฑ์Šค ๊ฐ’๋ณด๋‹ค ์ž‘์„ ๋•Œ๋Š” duration์„, ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ ์ธ๋ฑ์Šค ๊ฐ’ - ํ˜„์žฌ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋”ํ•ด์ฃผ๋„๋ก ํ–ˆ๋‹ค. ์ด ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ์„ ๋•Œ ๋ฌธ์ œ์ ์€ if๋ฌธ์œผ๋กœ ํ•ญ์ƒ ์กฐ๊ฑด์„ ํ™•์ธํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

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

๊ตณ์ด enumerate๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค ๋ฐ”๋กœ ์ „๊นŒ์ง€ ๋Œ๋‹ค๊ฐ€ ๋งˆ์ง€๋ง‰์— duration์„ ๋”ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฑฐ์˜€๋‹ค. ๋˜ ๋งค๋ฒˆ if๋ฌธ์œผ๋กœ ๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š๊ณ  min()์„ ์ด์šฉํ•ด ๋‘ ๊ฐ’ ์ค‘ ๋” ์ž‘์€ ๊ฐ’์„ total์— ๋”ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.

References


Written by@ugaemi
Record things I want to remember

๐Ÿฑ GitHub๐Ÿ“š Reading Space