[๋ฌธ์ ๋งํฌ] ๐
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก, ์ฑ์ฉ๊น์ง Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
ํ์ด ๋ฐฉ๋ฒ
๐กQueue ์ด์ฉ
- cards1์ cards2๋ฅผ ๊ฐ๊ฐ ํ์ฒ๋ผ ์ฌ์ฉํ์ฌ, ์์์๋ถํฐ ๋จ์ด๋ฅผ ๊บผ๋
- goal ๋ฐฐ์ด์ ๊ฐ ๋จ์ด์ ๋ํด
- ํด๋น ๋จ์ด๊ฐ cards1์ ๋งจ ์์ ์๋ค๋ฉด cards1์์ ์ ๊ฑฐ
- ๊ทธ๋ ์ง ์๊ณ cards2์ ๋งจ ์์ ์๋ค๋ฉด cards2์์ ์ ๊ฑฐ
- ๋ ๊ณณ ๋ชจ๋์ ์๋ค๋ฉด "No"๋ฅผ ๋ฐํ
- ๋ชจ๋ ๋จ์ด๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์ฒ๋ฆฌํ๋ค๋ฉด "Yes"๋ฅผ ๋ฐํ
Solution
from collections import deque
def solution(cards1, cards2, goal):
answer = "Yes"
cards1 = deque(cards1) # ํ ์์ฑ
cards2 = deque(cards2)
for i in goal:
if cards1 and i == cards1[0]:
cards1.popleft()
elif cards2 and i == cards2[0]:
cards2.popleft()
else:
answer = "No"
break
return answer
๐ฉ๐ป ํ๊ณ
IndexError ๊ฐ ๋ฌ ์ด์
if cards1 and i in cards1[0]:
cards1.popleft()
elif cards2 and i in cards2[0]:
cards2.popleft()
์นด๋ ์์๊ฐ ์ผ์นํด์ผ ํ๋๋ฐ ๋ฆฌ์คํธ์ ์๋์ง ์๋์ง๋ง ํ๋จํ๋ค. ๊ทธ๋์ card2๊ฐ ๋น์ด์๋๋ฐ 0๋ฒ์งธ ์ธ๋ฑ์ค์ ์๋์ง ์ฐพ์ผ๋ ค๊ณ ํ๋ IndexError๊ฐ ๋ฌ ๊ฒ์ด์๋ค.
'๐งฉ Algorithm > [Programmers] Level 1' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Programmers] L1. ์ถ์ต ์ ์ (Python) (1) | 2024.11.16 |
|---|---|
| [Programmers] L1. [1์ฐจ] ๋น๋ฐ์ง๋ (Python) (1) | 2024.11.12 |
| [Programmers] L1. ํธ๋ ํ์ดํธ ๋ํ (Python) (0) | 2024.11.11 |
| [Programmers] L1. ์ฝ๋ผ ๋ฌธ์ (Python) (0) | 2024.11.07 |
| [Programmers] L1. ๋ชจ์๊ณ ์ฌ (์์ ํ์/Python) (0) | 2024.11.06 |