ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

๐Ÿšฉ ๋ธŒ๋ฃจํŠธํฌ์Šค ์™„์ „ํƒ์ƒ‰ ๋ฌธ์ œ

 

thinking

3๊ฐœ๋ฅผ ๊ณจ๋ผ์•ผ ํ•˜๋ฏ€๋กœ 3์ค‘ for๋ฌธ์„ ๋Œ๋ฉด์„œ ์ฒซ๋ฒˆ์งธ ์นด๋“œ ๋ฝ‘๊ณ , ๊ทธ ์ดํ›„ ์นด๋“œ๋“ค ์ค‘์—์„œ ๋‘๋ฒˆ์งธ ์นด๋“œ ๋ฝ‘๊ณ , ๋‘๋ฒˆ์งธ์นด๋“œ ์ดํ›„ ์นด๋“œ๋“ค ์ค‘์—์„œ ๋งˆ์ง€๋ง‰ ์นด๋“œ๋ฅผ ๋ฝ‘์•„ M๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๋Š”๋‹ค.

๊ทธ ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ ์ค‘ max ๊ฐ’์„ ๋ฝ‘์œผ๋ฉด ๋œ๋‹ค.

 

์ฝ”๋“œ

N, M = map(int, input().split())
card = list(map(int, input().split()))

res = 0  # ๊ฒฐ๊ณผ๊ฐ’ ์ดˆ๊ธฐํ™”
# for ๋ฌธ ๋Œ๋ฉด์„œ ๋น„๊ต๊ฒ€์‚ฌ
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if card[i]+card[j]+card[k] <= M:
                res = max(res, card[i] + card[j] + card[k])
print(res)

 

itertools_combinations ์‚ฌ์šฉ ver

from itertools import combinations


N, M = map(int, input().split())
card_lst = list(map(int, input().split()))

max_value = 0  # ๊ฒฐ๊ณผ๊ฐ’ ์ดˆ๊ธฐํ™”
for card in combinations(card_lst, 3):
    # card = (5, 6, 7) <class 'tuple'>
    tmp = sum(card)
    if max_value < tmp <= M:
        max_value = tmp

print(max_value)

 

 

ํ•œ์ค„ํ›„๊ธฐ

N(3 ≤ N ≤ 100) & M(10 ≤ M ≤ 300,000) ์ด์–ด์„œ ์™„ํƒํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚  ์ค„ ์•Œ์•˜๋Š”๋ฐ ์•ˆ๋‚ฌ์Œ

๋Œ“๊ธ€