Post

๐ŸฆŠ ํƒ๋ฐฐ ๋ฐฐ๋‹ฌ๊ณผ ์ˆ˜๊ฑฐํ•˜๊ธฐ

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.