1) ๋ฌธ์ ์ค๋ช
- ์ด์์ฒด์ ์ ์ญํ ์ค ํ๋๋ ์ปดํจํฐ ์์คํ ์ ์์์ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๋ ๊ฒ์ ๋๋ค.
- ์ด ๋ฌธ์ ์์๋ ์ด์์ฒด์ ๊ฐ ๋ค์ ๊ท์น์ ๋ฐ๋ผ ํ๋ก์ธ์ค๋ฅผ ๊ด๋ฆฌํ ๊ฒฝ์ฐ ํน์ ํ๋ก์ธ์ค๊ฐ ๋ช ๋ฒ์งธ๋ก ์คํ๋๋์ง ์์๋ด๋ฉด ๋ฉ๋๋ค.
1. ์คํ ๋๊ธฐ ํ(Queue)์์ ๋๊ธฐ์ค์ธ ํ๋ก์ธ์ค ํ๋๋ฅผ ๊บผ๋
๋๋ค.
2. ํ์ ๋๊ธฐ์ค์ธ ํ๋ก์ธ์ค ์ค ์ฐ์ ์์๊ฐ ๋ ๋์ ํ๋ก์ธ์ค๊ฐ ์๋ค๋ฉด ๋ฐฉ๊ธ ๊บผ๋ธ ํ๋ก์ธ์ค๋ฅผ ๋ค์ ํ์ ๋ฃ์ต๋๋ค.
3. ๋ง์ฝ ๊ทธ๋ฐ ํ๋ก์ธ์ค๊ฐ ์๋ค๋ฉด ๋ฐฉ๊ธ ๊บผ๋ธ ํ๋ก์ธ์ค๋ฅผ ์คํํฉ๋๋ค.
3.1 ํ ๋ฒ ์คํํ ํ๋ก์ธ์ค๋ ๋ค์ ํ์ ๋ฃ์ง ์๊ณ ๊ทธ๋๋ก ์ข
๋ฃ๋ฉ๋๋ค.
- ์๋ฅผ ๋ค์ด ํ๋ก์ธ์ค 4๊ฐ [A, B, C, D]๊ฐ ์์๋๋ก ์คํ ๋๊ธฐ ํ์ ๋ค์ด์๊ณ , ์ฐ์ ์์๊ฐ [2, 1, 3, 2]๋ผ๋ฉด [C, D, A, B] ์์ผ๋ก ์คํํ๊ฒ ๋ฉ๋๋ค.
- ํ์ฌ ์คํ ๋๊ธฐ ํ(Queue)์ ์๋ ํ๋ก์ธ์ค์ ์ค์๋๊ฐ ์์๋๋ก ๋ด๊ธด ๋ฐฐ์ด priorities์, ๋ช ๋ฒ์งธ๋ก ์คํ๋๋์ง ์๊ณ ์ถ์ ํ๋ก์ธ์ค์ ์์น๋ฅผ ์๋ ค์ฃผ๋ location์ด ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ํด๋น ํ๋ก์ธ์ค๊ฐ ๋ช ๋ฒ์งธ๋ก ์คํ๋๋์ง return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
2) ์ ํ ์ฌํญ
- priorities์ ๊ธธ์ด๋ 1 ์ด์ 100 ์ดํ์ ๋๋ค.
- priorities์ ์์๋ 1 ์ด์ 9 ์ดํ์ ์ ์์ ๋๋ค.
- priorities์ ์์๋ ์ฐ์ ์์๋ฅผ ๋ํ๋ด๋ฉฐ ์ซ์๊ฐ ํด ์๋ก ์ฐ์ ์์๊ฐ ๋์ต๋๋ค.
- location์ 0 ์ด์ (๋๊ธฐ ํ์ ์๋ ํ๋ก์ธ์ค ์ - 1) ์ดํ์ ๊ฐ์ ๊ฐ์ง๋๋ค.
- priorities์ ๊ฐ์ฅ ์์ ์์ผ๋ฉด 0, ๋ ๋ฒ์งธ์ ์์ผ๋ฉด 1 โฆ ๊ณผ ๊ฐ์ด ํํํฉ๋๋ค.
3) ์ ์ถ๋ ฅ ์์
priorities location return
[2, 1, 3, 2] 2 1
[1, 1, 9, 1, 1, 1] 0 5
4) ์ ์ถ๋ ฅ ์์ ์ค๋ช
์์ #1
๋ฌธ์ ์ ๋์จ ์์ ๊ฐ์ต๋๋ค.
์์ #2
6๊ฐ์ ํ๋ก์ธ์ค [A, B, C, D, E, F]๊ฐ ๋๊ธฐ ํ์ ์๊ณ ์ค์๋๊ฐ [1, 1, 9, 1, 1, 1] ์ด๋ฏ๋ก [C, D, E, F, A, B] ์์ผ๋ก ์คํ๋ฉ๋๋ค. ๋ฐ๋ผ์ A๋ 5๋ฒ์งธ๋ก ์คํ๋ฉ๋๋ค.
5) ํ์ด
from collections import deque
def solution(priorities, location):
# Queue ์์ฑ
procQueue = deque()
# Queue ๋ฐ์ดํฐ ์ด๊ธฐํ + location ์ธ๋ฑ์ค๋ 1๋ก ํ๊ธฐ
for idx, n in enumerate(priorities):
if idx == location:
procQueue.append((n, 1))
else:
procQueue.append((n, 0))
# priorities ๋ฐฐ์ด ์ ๋ ฌ by ์ฐ์ ์์
priorities.sort()
# ์คํ ์์ ๋ณ์
count = 0
while procQueue:
maxPriority = priorities.pop()
proc, target = procQueue.popleft()
# ๊บผ๋ธ ํ๋ก์ธ์ค๊ฐ ์ฐ์ ์์๊ฐ ๊ฐ์ฅ ๋์ ๊ฒฝ์ฐ
if maxPriority == proc:
# ์คํ ์์ +1
count += 1
# ํด๋น ๋ฐ์ดํฐ๊ฐ location ์ธ๋ฑ์ค์ธ ๊ฒฝ์ฐ
if target == 1:
break
else:
# ์ฐ์ ์์๊ฐ ๋์ง ์์ ํ๋ก์ธ์ค๋ ๋ค์ ํ์ enqueue
priorities.append(maxPriority)
procQueue.append((proc, target))
return count
[Reference]
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๋ฐ์ํ
'๐ฏ Coding Test > ์๊ณ ๋ฆฌ์ฆ ํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค (Lv 2) - ์นดํซ (1) | 2023.08.17 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค (Lv 2) - ๊ธฐ๋ฅ๊ฐ๋ฐ (1) | 2023.08.17 |
ํ๋ก๊ทธ๋๋จธ์ค (Lv 0) - ์น์์ด (1) (1) | 2023.08.16 |
ํ๋ก๊ทธ๋๋จธ์ค (Lv 0) - ์ ์๋ฅผ ๋์ ํ์ผ๋ก ๋ฐฐ์นํ๊ธฐ (2) | 2023.08.16 |
๋ฐฑ์ค 2178๋ฒ - ๋ฏธ๋ก ํ์ (1) | 2023.08.14 |
1) ๋ฌธ์ ์ค๋ช
- ์ด์์ฒด์ ์ ์ญํ ์ค ํ๋๋ ์ปดํจํฐ ์์คํ ์ ์์์ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๋ ๊ฒ์ ๋๋ค.
- ์ด ๋ฌธ์ ์์๋ ์ด์์ฒด์ ๊ฐ ๋ค์ ๊ท์น์ ๋ฐ๋ผ ํ๋ก์ธ์ค๋ฅผ ๊ด๋ฆฌํ ๊ฒฝ์ฐ ํน์ ํ๋ก์ธ์ค๊ฐ ๋ช ๋ฒ์งธ๋ก ์คํ๋๋์ง ์์๋ด๋ฉด ๋ฉ๋๋ค.
1. ์คํ ๋๊ธฐ ํ(Queue)์์ ๋๊ธฐ์ค์ธ ํ๋ก์ธ์ค ํ๋๋ฅผ ๊บผ๋
๋๋ค.
2. ํ์ ๋๊ธฐ์ค์ธ ํ๋ก์ธ์ค ์ค ์ฐ์ ์์๊ฐ ๋ ๋์ ํ๋ก์ธ์ค๊ฐ ์๋ค๋ฉด ๋ฐฉ๊ธ ๊บผ๋ธ ํ๋ก์ธ์ค๋ฅผ ๋ค์ ํ์ ๋ฃ์ต๋๋ค.
3. ๋ง์ฝ ๊ทธ๋ฐ ํ๋ก์ธ์ค๊ฐ ์๋ค๋ฉด ๋ฐฉ๊ธ ๊บผ๋ธ ํ๋ก์ธ์ค๋ฅผ ์คํํฉ๋๋ค.
3.1 ํ ๋ฒ ์คํํ ํ๋ก์ธ์ค๋ ๋ค์ ํ์ ๋ฃ์ง ์๊ณ ๊ทธ๋๋ก ์ข
๋ฃ๋ฉ๋๋ค.
- ์๋ฅผ ๋ค์ด ํ๋ก์ธ์ค 4๊ฐ [A, B, C, D]๊ฐ ์์๋๋ก ์คํ ๋๊ธฐ ํ์ ๋ค์ด์๊ณ , ์ฐ์ ์์๊ฐ [2, 1, 3, 2]๋ผ๋ฉด [C, D, A, B] ์์ผ๋ก ์คํํ๊ฒ ๋ฉ๋๋ค.
- ํ์ฌ ์คํ ๋๊ธฐ ํ(Queue)์ ์๋ ํ๋ก์ธ์ค์ ์ค์๋๊ฐ ์์๋๋ก ๋ด๊ธด ๋ฐฐ์ด priorities์, ๋ช ๋ฒ์งธ๋ก ์คํ๋๋์ง ์๊ณ ์ถ์ ํ๋ก์ธ์ค์ ์์น๋ฅผ ์๋ ค์ฃผ๋ location์ด ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ํด๋น ํ๋ก์ธ์ค๊ฐ ๋ช ๋ฒ์งธ๋ก ์คํ๋๋์ง return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
2) ์ ํ ์ฌํญ
- priorities์ ๊ธธ์ด๋ 1 ์ด์ 100 ์ดํ์ ๋๋ค.
- priorities์ ์์๋ 1 ์ด์ 9 ์ดํ์ ์ ์์ ๋๋ค.
- priorities์ ์์๋ ์ฐ์ ์์๋ฅผ ๋ํ๋ด๋ฉฐ ์ซ์๊ฐ ํด ์๋ก ์ฐ์ ์์๊ฐ ๋์ต๋๋ค.
- location์ 0 ์ด์ (๋๊ธฐ ํ์ ์๋ ํ๋ก์ธ์ค ์ - 1) ์ดํ์ ๊ฐ์ ๊ฐ์ง๋๋ค.
- priorities์ ๊ฐ์ฅ ์์ ์์ผ๋ฉด 0, ๋ ๋ฒ์งธ์ ์์ผ๋ฉด 1 โฆ ๊ณผ ๊ฐ์ด ํํํฉ๋๋ค.
3) ์ ์ถ๋ ฅ ์์
priorities location return
[2, 1, 3, 2] 2 1
[1, 1, 9, 1, 1, 1] 0 5
4) ์ ์ถ๋ ฅ ์์ ์ค๋ช
์์ #1
๋ฌธ์ ์ ๋์จ ์์ ๊ฐ์ต๋๋ค.
์์ #2
6๊ฐ์ ํ๋ก์ธ์ค [A, B, C, D, E, F]๊ฐ ๋๊ธฐ ํ์ ์๊ณ ์ค์๋๊ฐ [1, 1, 9, 1, 1, 1] ์ด๋ฏ๋ก [C, D, E, F, A, B] ์์ผ๋ก ์คํ๋ฉ๋๋ค. ๋ฐ๋ผ์ A๋ 5๋ฒ์งธ๋ก ์คํ๋ฉ๋๋ค.
5) ํ์ด
from collections import deque
def solution(priorities, location):
# Queue ์์ฑ
procQueue = deque()
# Queue ๋ฐ์ดํฐ ์ด๊ธฐํ + location ์ธ๋ฑ์ค๋ 1๋ก ํ๊ธฐ
for idx, n in enumerate(priorities):
if idx == location:
procQueue.append((n, 1))
else:
procQueue.append((n, 0))
# priorities ๋ฐฐ์ด ์ ๋ ฌ by ์ฐ์ ์์
priorities.sort()
# ์คํ ์์ ๋ณ์
count = 0
while procQueue:
maxPriority = priorities.pop()
proc, target = procQueue.popleft()
# ๊บผ๋ธ ํ๋ก์ธ์ค๊ฐ ์ฐ์ ์์๊ฐ ๊ฐ์ฅ ๋์ ๊ฒฝ์ฐ
if maxPriority == proc:
# ์คํ ์์ +1
count += 1
# ํด๋น ๋ฐ์ดํฐ๊ฐ location ์ธ๋ฑ์ค์ธ ๊ฒฝ์ฐ
if target == 1:
break
else:
# ์ฐ์ ์์๊ฐ ๋์ง ์์ ํ๋ก์ธ์ค๋ ๋ค์ ํ์ enqueue
priorities.append(maxPriority)
procQueue.append((proc, target))
return count
[Reference]
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๋ฐ์ํ
'๐ฏ Coding Test > ์๊ณ ๋ฆฌ์ฆ ํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค (Lv 2) - ์นดํซ (1) | 2023.08.17 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค (Lv 2) - ๊ธฐ๋ฅ๊ฐ๋ฐ (1) | 2023.08.17 |
ํ๋ก๊ทธ๋๋จธ์ค (Lv 0) - ์น์์ด (1) (1) | 2023.08.16 |
ํ๋ก๊ทธ๋๋จธ์ค (Lv 0) - ์ ์๋ฅผ ๋์ ํ์ผ๋ก ๋ฐฐ์นํ๊ธฐ (2) | 2023.08.16 |
๋ฐฑ์ค 2178๋ฒ - ๋ฏธ๋ก ํ์ (1) | 2023.08.14 |