๐ฆ ํ๋ฐฐ ๋ฐฐ๋ฌ๊ณผ ์๊ฑฐํ๊ธฐ
1. ๋ฌธ์ ๋งํฌ
2023 KAKAO BLIND RECRUITMENT: ํ๋ฐฐ ๋ฐฐ๋ฌ๊ณผ ์๊ฑฐํ๊ธฐ
2. ์ฝ๋
์คํ ๊ฒฐ๊ณผ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
์ฑ์ ์ ์์ํฉ๋๋ค. ์ ํ์ฑ ํ ์คํธ ํ ์คํธ 1 ใ ํต๊ณผ (0.02ms, 10.1MB) ํ ์คํธ 2 ใ ํต๊ณผ (0.01ms, 10.3MB) ํ ์คํธ 3 ใ ํต๊ณผ (0.04ms, 10.4MB) ํ ์คํธ 4 ใ ํต๊ณผ (0.02ms, 10.4MB) ํ ์คํธ 5 ใ ํต๊ณผ (0.05ms, 10.1MB) ํ ์คํธ 6 ใ ํต๊ณผ (0.03ms, 10.1MB) ํ ์คํธ 7 ใ ํต๊ณผ (0.77ms, 10.4MB) ํ ์คํธ 8 ใ ํต๊ณผ (3.28ms, 10.5MB) ํ ์คํธ 9 ใ ํต๊ณผ (7.97ms, 10.3MB) ํ ์คํธ 10 ใ ํต๊ณผ (6.25ms, 10.3MB) ํ ์คํธ 11 ใ ํต๊ณผ (3.26ms, 10.4MB) ํ ์คํธ 12 ใ ํต๊ณผ (2.60ms, 10.4MB) ํ ์คํธ 13 ใ ํต๊ณผ (2.26ms, 10.4MB) ํ ์คํธ 14 ใ ํต๊ณผ (2.25ms, 10.3MB) ํ ์คํธ 15 ใ ํต๊ณผ (34.50ms, 15.8MB) ํ ์คํธ 16 ใ ํต๊ณผ (1845.92ms, 18.8MB) ํ ์คํธ 17 ใ ํต๊ณผ (162.40ms, 18.1MB) ํ ์คํธ 18 ใ ํต๊ณผ (95.86ms, 19.5MB) ํ ์คํธ 19 ใ ํต๊ณผ (62.95ms, 17.3MB) ํ ์คํธ 20 ใ ํต๊ณผ (77.38ms, 18.9MB) ์ฑ์ ๊ฒฐ๊ณผ ์ ํ์ฑ: 100.0 ํฉ๊ณ: 100.0 / 100.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# ์ค๋ฅธ์ชฝ๋ถํฐ ๋จ์ ๊ฐ์ ๊ฐ์
def minus_cap(cap, n, indexes, arr):
left_cap = cap
while indexes and left_cap > 0:
idx = indexes[-1]
# ์์ง ๋จ์ ์ํ
if arr[idx] > left_cap:
arr[idx] -= left_cap
left_cap = 0
else:
left_cap -= arr[idx]
arr[idx] = 0
indexes.pop()
# ๋ฐฐ๋ฌํ ๊ฐ์๋ฅผ ์ค๋ฅธ์ชฝ๋ถํฐ ๊ฐ์์ํด
def solution(cap, n, deliveries, pickups):
answer = 0
# ๋ฐฐ๋ฌํ ์ธ๋ฑ์ค ์ญ์
delivery_idx = []
for i in range(n):
if deliveries[i] > 0:
delivery_idx.append(i)
# ํฝ์
์ธ๋ฑ์ค ์ญ์
pickup_idx = []
for i in range(n):
if pickups[i] > 0:
pickup_idx.append(i)
while True:
current_delivery_idx = -1
current_pickup_idx = -1
if delivery_idx:
current_delivery_idx = delivery_idx[-1]
minus_cap(cap, n, delivery_idx, deliveries)
if pickup_idx:
current_pickup_idx = pickup_idx[-1]
minus_cap(cap, n, pickup_idx, pickups)
# ๋์ด์ ๋จ์๊ฒ ์์ผ๋ฉด ์ข
๋ฃ
if current_delivery_idx == -1 and current_pickup_idx == -1:
break
# ํ์ชฝ๋ง ๋จ์ ๊ฒฝ์ฐ ๋ณด์
if current_delivery_idx == -1:
current_delivery_idx = current_pickup_idx
if current_pickup_idx == -1:
current_pickup_idx = current_delivery_idx
answer += current_delivery_idx + 1 + current_pickup_idx + 1 + abs(current_delivery_idx - current_pickup_idx)
return answer
ํด์ค
ํญ์ ๋ฐฐ๋ฌ โ ํฝ์ ์์ผ๋ก ์ด๋ฃจ์ด์ง
์์์์๋ ๋ณผ ์ ์๋ฏ์ด ๊ฐ์ฅ ๋จผ ๊ฑฐ๋ฆฌ์ ์๋ ์ง ๋ถํฐ ๋ฐฐ๋ฌ๊ณผ ํฝ์ ์ด ์ด๋ฃจ์ด์ง๋ค๊ณ ํ ์ ์์
์์๊ฐ ์กด์ฌํ๋ idx ๋ง ์ ์ฅ
๊ฐ ๋ฐฐ์ด์ ์ ์ผ ์ค๋ฅธ์ชฝ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ ธ์ด
๊ทธ ์ธ๋ฑ์ค๋ถํฐ ์ญ์์ผ๋ก ๋จ์ cap ๋งํผ ์์ ๊ฐ์ ๊ฐ์์ํด
answer ์ ๋ฐฐ๋ฌidx, ํฝ์ idx, (๋ฐฐ๋ฌidx - ํฝ์ idx) ๋ฅผ ๋ํจ
This post is licensed under CC BY 4.0 by the author.