๋ฌธ์
We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves with a speed of 1 unit per second. Some of the ants move to the left, the other move to the right.
When two ants moving in two different directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time.
When an ant reaches one end of the plank at a time t, it falls out of the plank immediately.
Given an integer n and two integer arrays left and right, the positions of the ants moving to the left and the right, return the moment when the last ant(s) fall out of the plank.
Input: n = 4, left = [4,3], right = [0,1]
Output: 4
Explanation: In the image above:
-The ant at index 0 is named A and going to the right.
-The ant at index 1 is named B and going to the right.
-The ant at index 3 is named C and going to the left.
-The ant at index 4 is named D and going to the left.
The last moment when an ant was on the plank is t = 4 seconds. After that, it falls immediately out of the plank. (i.e., We can say that at t = 4.0000000001, there are no ants on the plank).
ํด์ค ์ฝ๋
class Solution(object):
def getLastMoment(self, n, left, right):
if not right:
return max(left)
if not left:
return n - min(right)
l = max(left)
r = n - min(right)
return max(l,r)
๋ถ์ > ํ๊ณ ๋๋ ๋๋ฌด ์ฌ์ด ์์์ ์ธ ๋ฌธ์ ๋ผ ํ๋ฌดํ๋ค. ์ผ์ชฝ์์ ์ค๋ ๊ฐ๋ฏธ์ ์ค๋ฅธ์ชฝ์์ ์ค๋ ๊ฐ๋ฏธ๊ฐ ๊ฒน์น๋ ๊ฑด ์ ์ด์ ๊ณ ๋ คํ์ง ์์๋ ๋๊ณ . ๋ถ๋ชํ๋ ๋ง๋ ๊ฐ๋ฏธ๋ ์๋ ์๊ธฐ๊ฐ ๊ฐ๋ ค๋ ๋ฐฉํฅ์ผ๋ก ๊ฐ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๊ฐ์ฅ ์ผ์ชฝ์ ์๋ ๊ฐ๋ฏธ๊ฐ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ ๋์ฐฉํ๋ ์๊ฐ๊ณผ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ ์๋ ๊ฐ๋ฏธ๊ฐ ๊ฐ์ฅ ์ผ์ชฝ์ ๋์ฐฉํ๋ ์๊ฐ ์ค์ ๋ ํฐ ๊ฐ์ ๋ฐํํ๋ฉด, ๊ทธ ๊ฐ์ด ๋ง์ง๋ง ๊ฐ๋ฏธ๊ฐ ๋จ์ด์ง๋ ์๊ฐ์ด์๋ค.
'Algorithm > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋งค์ผ LeetCode ํ๊ธฐ | Two Sum / Time to Buy and Sell Stock (0) | 2024.08.12 |
---|---|
[Leetcode] Group Anagrams (0) | 2022.05.18 |
[Leetcode] Validate Stack Sequences (#946) (0) | 2022.04.21 |
[Leetcode] Create Target Array in the Given Order (#1389) (0) | 2022.04.21 |
[Leetcode] Clumsy Factorial (#1006) (0) | 2022.04.21 |