728x90
[๋ฌธ์ ๋งํฌ] ๐
SW Expert Academy
SW ํ๋ก๊ทธ๋๋ฐ ์ญ๋ ๊ฐํ์ ๋์์ด ๋๋ ๋ค์ํ ํ์ต ์ปจํ ์ธ ๋ฅผ ํ์ธํ์ธ์!
swexpertacademy.com
ํ์ด
๐ก์นด๋๋ฅผ ์ ๋ฐ์ผ๋ก ๋๋ ์ ๊ฐ์ ์ ์ฅํ ํ ๊ฒฐ๊ณผ ๋ฑ์ ์์ฑํ๋ค. ํ์์ผ ๊ฒฝ์ฐ ๋ง์ง๋ง ๋จ์ ์นด๋๋ฅผ ์ถ๊ฐํ๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ์๋ ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ๋ค์ด ์๋ค.
1๏ธโฃ pop() ์ฌ์ฉ
while len(second_half) > 0:
result.append(first_half.pop(0)) # ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋นผ์ ๊ฒฐ๊ณผ์ ๋ฃ๊ธฐ
result.append(second_half.pop(0))
- pop() ์ ์ฌ์ฉํ๋ฉด ์ฑ๋ฅ์ด ๋ค์ ์ ํ๋ ์ ์๋ค.
- ๋ฆฌ์คํธ์ ๋งจ ์ ์์๋ฅผ ์ ๊ฑฐํ ๋๋ง๋ค ๋๋จธ์ง ์์๋ฅผ ์์ผ๋ก ๋น๊ฒจ์ผ ํ๋ฏ๋ก, ์ฑ๋ฅ ์ ๋นํจ์จ์ ์ด๋ค.
2๏ธโฃ ๋ฑ ์ฌ์ฉ
while second_half:
result.append(first_half.popleft())
result.append(second_half.popleft())
- deque ์ ํ์ฉํด popleft() ๋ก ๋ฆฌ์คํธ์ ์์์ ์์๋ฅผ ์ ๊ฑฐํ๋ ๋น์ฉ์ O(1)๋ก ์ค์๋ค.
- pop() ์ ์ฌ์ฉํ๋ ๊ฒ ๋ณด๋ค ํจ์ฌ ํจ์จ์ ์ด๋ค.
3๏ธโฃ ๋ฆฌ์คํธ๋ฅผ ๊ต์ฐจ๋ก ํฉ์น๊ธฐ
for i in range(len(second_half)):
result.append(first_half[i])
result.append(second_half[i])
- ๋จ์ํ ๋ ๋ฆฌ์คํธ๋ฅผ for ๋ฌธ์ผ๋ก ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ๋ ์๋ค.
- ์์ ๋ ๋ฐฉ๋ฒ๋ณด๋ค ์ฝ๋๊ฐ ํจ์ฌ ๊ฐ๊ฒฐํ๊ณ ์คํ ์๋๋ ๋น ๋ฅด๋ค.
Solution
import math
T = int(input()) # ํ
์คํธ ์ผ์ด์ค ์
for test_case in range(1, T + 1):
N = int(input()) # ์นด๋ ์
card = list(map(str, input().split()))
first_half = [card[i] for i in range(math.ceil(N / 2))] # ์นด๋์ ์ ๋ฐ์์ ์ ์นด๋๋ค๋ง ์ ์ฅ
second_half = [card[i] for i in range(math.ceil(N / 2), N)] # ์นด๋์ ์ ๋ฐ์์ ๋ค ์นด๋๋ค๋ง ์ ์ฅ
result = []
while len(second_half) > 0:
result.append(first_half.pop(0)) # ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋นผ์ ๊ฒฐ๊ณผ์ ๋ฃ๊ธฐ
result.append(second_half.pop(0))
# ํ์ ์ผ๋ first ๋ฆฌ์คํธ๋ ํ๋์ ์นด๋๊ฐ ๋จ๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์ถ๊ฐ
if N % 2 != 0:
result.append(first_half.pop())
print(f"#{test_case}", end=' ')
print(*result)
์์ ํ ์ฝ๋
from collections import deque
import math
T = int(input()) # ํ
์คํธ ์ผ์ด์ค ์
for test_case in range(1, T + 1):
N = int(input()) # ์นด๋ ์
card = input().split()
# ์ ๋ฐ์ผ๋ก ๋๋ ๋ฑ์ deque๋ก ๋ง๋ค์ด popleft()๋ก ํจ์จ์ ์ธ ์ ๊ฑฐ ์ํ
first_half = deque(card[:math.ceil(N / 2)])
second_half = deque(card[math.ceil(N / 2):])
result = []
# ๋ ๋ฐ์ชฝ์ ๊ต์ฐจ๋ก ์์ด ์๋ก์ด ๋ฑ์ ์์ฑ
while second_half:
result.append(first_half.popleft())
result.append(second_half.popleft())
# ํ์์ผ ๋ ์ฒซ ๋ฒ์งธ ๋ฐ์ชฝ์ ํ ์ฅ ๋ ๋จ์์๋ ๊ฒฝ์ฐ ์ถ๊ฐ
if first_half:
result.append(first_half.popleft())
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
print(f"#{test_case} {' '.join(result)}")
ํจ์จ์ ์ธ ์ฝ๋
T = int(input()) # ํ
์คํธ ์ผ์ด์ค ์
for test_case in range(1, T + 1):
N = int(input()) # ์นด๋ ์
card = input().split()
# ์นด๋ ๋ฑ์ ์ ๋ฐ์ผ๋ก ๋๋๊ธฐ
mid = (N + 1) // 2 # ํ์์ผ ๋ ์์ชฝ ๋ฑ์ ํ๋ ๋ ๋ฐฐ์
first_half = card[:mid]
second_half = card[mid:]
# ๊ฒฐ๊ณผ ๋ฑ ์์ฑ
result = []
for i in range(len(second_half)):
result.append(first_half[i])
result.append(second_half[i])
# ํ์์ผ ๊ฒฝ์ฐ ๋ง์ง๋ง ๋จ์ ์นด๋ ์ถ๊ฐ
if N % 2 != 0:
result.append(first_half[-1])
print(f"#{test_case} {' '.join(result)}")
๐ฉ๐ป ํ๊ณ
์นด๋๋ฅผ ์ ๋ฐ์ผ๋ก ๋๋ ๋๋ถํฐ ๋๊ฐ ๊ตณ์ ๊ฒ ๊ฐ๋ค. ํ์์ผ ๊ฒฝ์ฐ ์ํ๋๋๋ก ๋๋ ์ง์ง ์์๊ธฐ ๋๋ฌธ์ด๋ค.
ํ์ด๊ฐ ๋ค์ํ๋ฐ ๋ด ์ฝ๋๋ ํจ์จ์ ์ด์ง ๋ชปํ๋ค. ์ฑ๋ฅ ์๊ฐ๋ ํ๋ฉด์ ์ฝ๋๋ฅผ ์ง๋ ๋ ธ๋ ฅ์ด ํ์ํ๋ค.
728x90
'๐งฉ Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [SWEA] 5356. ์์์ด์ ์ธ๋ก๋ก ๋งํด์ (Python/D3) (1) | 2024.10.29 |
|---|---|
| [SWEA] 11736. ํ๋ฒํ ์ซ์ (Python/D3) (0) | 2024.10.29 |
| [SWEA] 1221. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 5์ผ์ฐจ - GNS (Python/D3) (0) | 2024.10.26 |
| [SWEA] 3142. ์์ค์ด์ ์ ๋นํ ๋ฟ์ ์ฒ (Python/D3) (0) | 2024.10.26 |
| [SWEA] 10200. ๊ตฌ๋ ์ ์ ์ (Python/D3) (0) | 2024.10.26 |