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.
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
์ ๋ํด์ฃผ๋ ๊ฒ์ด ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ํด์ค๋ค.