Post

๐Ÿข ์–‘๊ถ ๋Œ€ํšŒ

1. ๋ฌธ์ œ ๋งํฌ

์–‘๊ถ ๋Œ€ํšŒ



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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
์–‘๊ถ๋Œ€ํšŒ

๐Ÿ’› ๋ฌธ์ œ
์นด์นด์˜ค๋ฐฐ ์–‘๊ถ๋Œ€ํšŒ๊ฐ€ ์—ด๋ ธ์Šต๋‹ˆ๋‹ค.
๋ผ์ด์–ธ์€ ์ €๋ฒˆ ์นด์นด์˜ค๋ฐฐ ์–‘๊ถ๋Œ€ํšŒ ์šฐ์Šน์ž์ด๊ณ  ์ด๋ฒˆ ๋Œ€ํšŒ์—๋„ ๊ฒฐ์Šน์ „๊นŒ์ง€ ์˜ฌ๋ผ์™”์Šต๋‹ˆ๋‹ค. ๊ฒฐ์Šน์ „ ์ƒ๋Œ€๋Š” ์–ดํ”ผ์น˜์ž…๋‹ˆ๋‹ค.
์นด์นด์˜ค๋ฐฐ ์–‘๊ถ๋Œ€ํšŒ ์šด์˜์œ„์›ํšŒ๋Š” ํ•œ ์„ ์ˆ˜์˜ ์—ฐ์† ์šฐ์Šน๋ณด๋‹ค๋Š” ๋‹ค์–‘ํ•œ ์„ ์ˆ˜๋“ค์ด ์–‘๊ถ๋Œ€ํšŒ์—์„œ ์šฐ์Šนํ•˜๊ธฐ๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ, ์–‘๊ถ๋Œ€ํšŒ ์šด์˜์œ„์›ํšŒ๋Š” ๊ฒฐ์Šน์ „ ๊ทœ์น™์„ ์ „ ๋Œ€ํšŒ ์šฐ์Šน์ž์ธ ๋ผ์ด์–ธ์—๊ฒŒ ๋ถˆ๋ฆฌํ•˜๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

1. ์–ดํ”ผ์น˜๊ฐ€ ํ™”์‚ด n๋ฐœ์„ ๋‹ค ์œ ํ›„์— ๋ผ์ด์–ธ์ด ํ™”์‚ด n๋ฐœ์„ ์ฉ๋‹ˆ๋‹ค.
2. ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
    ๊ณผ๋…ํŒ์€ ์•„๋ž˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์ƒ๊ฒผ์œผ๋ฉฐ ๊ฐ€์žฅ ์ž‘์€ ์›์˜ ๊ณผ๋… ์ ์ˆ˜๋Š” 10์ ์ด๊ณ  ๊ฐ€์žฅ ํฐ ์›์˜ ๋ฐ”๊นฅ์ชฝ์€ ๊ณผ๋… ์ ์ˆ˜๊ฐ€ 0์ ์ž…๋‹ˆ๋‹ค.

    ๋งŒ์•ฝ, k(k๋Š” 1~10์‚ฌ์ด์˜ ์ž์—ฐ์ˆ˜)์ ์„ ์–ดํ”ผ์น˜๊ฐ€ a๋ฐœ์„ ๋งžํ˜”๊ณ  ๋ผ์ด์–ธ์ด b๋ฐœ์„ ๋งžํ˜”์„ ๊ฒฝ์šฐ ๋” ๋งŽ์€ ํ™”์‚ด์„ k์ ์— ๋งžํžŒ ์„ ์ˆ˜๊ฐ€ k ์ ์„ ๊ฐ€์ ธ๊ฐ‘๋‹ˆ๋‹ค.
    ๋‹จ, a = b์ผ ๊ฒฝ์šฐ๋Š” ์–ดํ”ผ์น˜๊ฐ€ k์ ์„ ๊ฐ€์ ธ๊ฐ‘๋‹ˆ๋‹ค.
    k์ ์„ ์—ฌ๋Ÿฌ ๋ฐœ ๋งžํ˜€๋„ k์  ๋ณด๋‹ค ๋งŽ์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ ธ๊ฐ€๋Š” ๊ฒŒ ์•„๋‹ˆ๊ณ  k์ ๋งŒ ๊ฐ€์ ธ๊ฐ€๋Š” ๊ฒƒ์„ ์œ ์˜ํ•˜์„ธ์š”.
    ๋˜ํ•œ a = b = 0 ์ธ ๊ฒฝ์šฐ, ์ฆ‰, ๋ผ์ด์–ธ๊ณผ ์–ดํ”ผ์น˜ ๋ชจ๋‘ k์ ์— ๋‹จ ํ•˜๋‚˜์˜ ํ™”์‚ด๋„ ๋งžํžˆ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ๋Š” ์–ด๋Š ๋ˆ„๊ตฌ๋„ k์ ์„ ๊ฐ€์ ธ๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

        ์˜ˆ๋ฅผ ๋“ค์–ด, ์–ดํ”ผ์น˜๊ฐ€ 10์ ์„ 2๋ฐœ ๋งžํ˜”๊ณ  ๋ผ์ด์–ธ๋„ 10์ ์„ 2๋ฐœ ๋งžํ˜”์„ ๊ฒฝ์šฐ ์–ดํ”ผ์น˜๊ฐ€ 10์ ์„ ๊ฐ€์ ธ๊ฐ‘๋‹ˆ๋‹ค.
        ๋‹ค๋ฅธ ์˜ˆ๋กœ, ์–ดํ”ผ์น˜๊ฐ€ 10์ ์„ 0๋ฐœ ๋งžํ˜”๊ณ  ๋ผ์ด์–ธ์ด 10์ ์„ 2๋ฐœ ๋งžํ˜”์„ ๊ฒฝ์šฐ ๋ผ์ด์–ธ์ด 10์ ์„ ๊ฐ€์ ธ๊ฐ‘๋‹ˆ๋‹ค.
3. ๋ชจ๋“  ๊ณผ๋… ์ ์ˆ˜์— ๋Œ€ํ•˜์—ฌ ๊ฐ ์„ ์ˆ˜์˜ ์ตœ์ข… ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

์ตœ์ข… ์ ์ˆ˜๊ฐ€ ๋” ๋†’์€ ์„ ์ˆ˜๋ฅผ ์šฐ์Šน์ž๋กœ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ตœ์ข… ์ ์ˆ˜๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ ์–ดํ”ผ์น˜๋ฅผ ์šฐ์Šน์ž๋กœ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
ํ˜„์žฌ ์ƒํ™ฉ์€ ์–ดํ”ผ์น˜๊ฐ€ ํ™”์‚ด n๋ฐœ์„ ๋‹ค ์œ ํ›„์ด๊ณ  ๋ผ์ด์–ธ์ด ํ™”์‚ด์„ ์  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค.
๋ผ์ด์–ธ์€ ์–ดํ”ผ์น˜๋ฅผ ๊ฐ€์žฅ ํฐ ์ ์ˆ˜ ์ฐจ์ด๋กœ ์ด๊ธฐ๊ธฐ ์œ„ํ•ด์„œ n๋ฐœ์˜ ํ™”์‚ด์„ ์–ด๋–ค ๊ณผ๋… ์ ์ˆ˜์— ๋งžํ˜€์•ผ ํ•˜๋Š”์ง€๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

ํ™”์‚ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‹ด์€ ์ž์—ฐ์ˆ˜ n, ์–ดํ”ผ์น˜๊ฐ€ ๋งžํžŒ ๊ณผ๋… ์ ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ 10์ ๋ถ€ํ„ฐ 0์ ๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด info๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
์ด๋•Œ, ๋ผ์ด์–ธ์ด ๊ฐ€์žฅ ํฐ ์ ์ˆ˜ ์ฐจ์ด๋กœ ์šฐ์Šนํ•˜๊ธฐ ์œ„ํ•ด n๋ฐœ์˜ ํ™”์‚ด์„ ์–ด๋–ค ๊ณผ๋… ์ ์ˆ˜์— ๋งžํ˜€์•ผ ํ•˜๋Š”์ง€๋ฅผ
10์ ๋ถ€ํ„ฐ 0์ ๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์ •์ˆ˜ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.
๋งŒ์•ฝ, ๋ผ์ด์–ธ์ด ์šฐ์Šนํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ(๋ฌด์กฐ๊ฑด ์ง€๊ฑฐ๋‚˜ ๋น„๊ธฐ๋Š” ๊ฒฝ์šฐ)๋Š” [-1]์„ return ํ•ด์ฃผ์„ธ์š”.

๐Ÿงก ์ œํ•œ ์‚ฌํ•ญ
1 โ‰ค n โ‰ค 10
info์˜ ๊ธธ์ด = 11
    0 โ‰ค info์˜ ์›์†Œ โ‰ค n
    info์˜ ์›์†Œ ์ดํ•ฉ = n
    info์˜ i๋ฒˆ์งธ ์›์†Œ๋Š” ๊ณผ๋…์˜ 10 - i ์ ์„ ๋งžํžŒ ํ™”์‚ด ๊ฐœ์ˆ˜์ž…๋‹ˆ๋‹ค. ( i๋Š” 0~10 ์‚ฌ์ด์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.)
๋ผ์ด์–ธ์ด ์šฐ์Šนํ•  ๋ฐฉ๋ฒ•์ด ์žˆ๋Š” ๊ฒฝ์šฐ, return ํ•  ์ •์ˆ˜ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 11์ž…๋‹ˆ๋‹ค.
    0 โ‰ค returnํ•  ์ •์ˆ˜ ๋ฐฐ์—ด์˜ ์›์†Œ โ‰ค n
    returnํ•  ์ •์ˆ˜ ๋ฐฐ์—ด์˜ ์›์†Œ ์ดํ•ฉ = n (๊ผญ n๋ฐœ์„ ๋‹ค ์ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.)
    returnํ•  ์ •์ˆ˜ ๋ฐฐ์—ด์˜ i๋ฒˆ์งธ ์›์†Œ๋Š” ๊ณผ๋…์˜ 10 - i ์ ์„ ๋งžํžŒ ํ™”์‚ด ๊ฐœ์ˆ˜์ž…๋‹ˆ๋‹ค. ( i๋Š” 0~10 ์‚ฌ์ด์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.)
    ๋ผ์ด์–ธ์ด ๊ฐ€์žฅ ํฐ ์ ์ˆ˜ ์ฐจ์ด๋กœ ์šฐ์Šนํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ ๊ฒฝ์šฐ, ๊ฐ€์žฅ ๋‚ฎ์€ ์ ์ˆ˜๋ฅผ ๋” ๋งŽ์ด ๋งžํžŒ ๊ฒฝ์šฐ๋ฅผ return ํ•ด์ฃผ์„ธ์š”.
        ๊ฐ€์žฅ ๋‚ฎ์€ ์ ์ˆ˜๋ฅผ ๋งžํžŒ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ ๊ณ„์†ํ•ด์„œ ๊ทธ๋‹ค์Œ์œผ๋กœ ๋‚ฎ์€ ์ ์ˆ˜๋ฅผ ๋” ๋งŽ์ด ๋งžํžŒ ๊ฒฝ์šฐ๋ฅผ return ํ•ด์ฃผ์„ธ์š”.
        ์˜ˆ๋ฅผ ๋“ค์–ด, [2,3,1,0,0,0,0,1,3,0,0]๊ณผ [2,1,0,2,0,0,0,2,3,0,0]๋ฅผ ๋น„๊ตํ•˜๋ฉด [2,1,0,2,0,0,0,2,3,0,0]๋ฅผ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
        ๋‹ค๋ฅธ ์˜ˆ๋กœ, [0,0,2,3,4,1,0,0,0,0,0]๊ณผ [9,0,0,0,0,0,0,0,1,0,0]๋ฅผ ๋น„๊ตํ•˜๋ฉด[9,0,0,0,0,0,0,0,1,0,0]๋ฅผ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋ผ์ด์–ธ์ด ์šฐ์Šนํ•  ๋ฐฉ๋ฒ•์ด ์—†๋Š” ๊ฒฝ์šฐ, return ํ•  ์ •์ˆ˜ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 1์ž…๋‹ˆ๋‹ค.
    ๋ผ์ด์–ธ์ด ์–ด๋–ป๊ฒŒ ํ™”์‚ด์„ ์˜๋“  ๋ผ์ด์–ธ์˜ ์ ์ˆ˜๊ฐ€ ์–ดํ”ผ์น˜์˜ ์ ์ˆ˜๋ณด๋‹ค ๋‚ฎ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด [-1]์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ’š ์ž…์ถœ๋ ฅ
n	info	                result
5	[2,1,1,1,0,0,0,0,0,0,0]	[0,2,2,0,1,0,0,0,0,0,0]
1	[1,0,0,0,0,0,0,0,0,0,0]	[-1]
9	[0,0,1,2,0,1,1,1,1,1,1]	[1,1,2,0,1,2,2,0,0,0,0]
10	[0,0,0,0,0,0,0,0,3,4,3]	[1,1,1,1,1,1,1,1,0,0,2]
"""

import copy

min_temp = -1e9
answers = []

# ๋ผ์ด์–ธ๊ณผ ์–ดํ”ผ์น˜์˜ ์ ์ˆ˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜


def CalScore(info, ryanShot):
    apeach, ryan = 0, 0

    for i in range(11):
        if (info[i], ryanShot[i]) == (0, 0):
            continue
        if info[i] >= ryanShot[i]:
            apeach += 10 - i
        else:
            ryan += 10 - i
    return ryan - apeach


def dfs(info, ryanShot, n, i):
    global min_temp, answers
    if i == 11:
        if n != 0:
            ryanShot[10] = n
        scoreDiff = CalScore(info, ryanShot)

        if scoreDiff <= 0:
            return
        result = copy.deepcopy(ryanShot)

        # ํ˜„์žฌ ๊ตฌํ•œ ์ตœ๋Œ€ ์ ์ˆ˜์ฐจ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ๋Š” ํ™”์‚ด ๊ฒฐ๊ณผ ๊ฒฝ์šฐ์˜ ์ˆ˜
        if scoreDiff > min_temp:
            # ์ตœ๋Œ€์ ์ˆ˜์ฐจ ๊ฐฑ์‹ 
            min_temp = scoreDiff
            # ์ด์ „ ๊ฒฐ๊ณผ๋“ค๋กœ ๋งŒ๋“ค์–ด์ง„ ์ตœ๋Œ€ ์ ์ˆ˜์ฐจ์™€ ๊ฐ™์€ ์ ์ˆ˜์ฐจ๊ฐ€ ๋‚˜๋Š” ๋ผ์ด์–ธ์˜ ํ™”์‚ด ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋ฉด answers ๋ฐฐ์—ด์— ์ถ”๊ฐ€
            answers = [result]
            return

        if scoreDiff == min_temp:
            answers.append(result)
        return

    # ์ ์ˆ˜ ๋จน๋Š” ๊ฒฝ์šฐ
    if info[i] < n:
        ryanShot.append(info[i] + 1)
        dfs(info, ryanShot, n - info[i] - 1, i + 1)
        ryanShot.pop()

    # ์ ์ˆ˜ ์•ˆ๋จน๋Š” ๊ฒฝ์šฐ
    ryanShot.append(0)
    dfs(info, ryanShot, n, i + 1)
    ryanShot.pop()


# ํ™”์‚ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‹ด์€ ์ž์—ฐ์ˆ˜ n, ์–ดํ”ผ์น˜๊ฐ€ ๋งžํžŒ ๊ณผ๋… ์ ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ 10์ ๋ถ€ํ„ฐ 0์ ๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด info
def solution(n, info):
    global min_temp, answers

    dfs(info, [], n, 0)

    # ๋ผ์ด์–ธ์ด ์šฐ์Šนํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ(๋ฌด์กฐ๊ฑด ์ง€๊ฑฐ๋‚˜ ๋น„๊ธฐ๋Š” ๊ฒฝ์šฐ)๋Š” [-1]์„ return
    if answers == []:
        return [-1]

    # ๊ฐ€์žฅ ํฐ ์ ์ˆ˜ ์ฐจ์ด๋กœ ์šฐ์Šนํ•˜๊ธฐ ์œ„ํ•ด n๋ฐœ์˜ ํ™”์‚ด์„ ์–ด๋–ค ๊ณผ๋… ์ ์ˆ˜์— ๋งžํ˜€์•ผ ํ•˜๋Š”์ง€๋ฅผ 10์ ๋ถ€ํ„ฐ 0์ ๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์ •์ˆ˜ ๋ฐฐ์—ด
    answers.sort(key=lambda x: x[::-1], reverse=True)
    return answers[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
    
      ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (6.83ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (5.62ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (2.86ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (4.52ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (4.95ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1.12ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.78ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (2.24ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.99ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.68ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (3.36ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (4.22ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (4.35ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (2.87ms, 10.5MB)
      ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (1.68ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (5.44ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (5.10ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (10.18ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.63ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (8.67ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (9.51ms, 10.4MB)
    


3. ํ•ด์„ค

This post is licensed under CC BY 4.0 by the author.