๐๋ฌธ์
https://www.acmicpc.net/problem/2798
๐์ด๋ค ์๊ณ ๋ฆฌ์ฆ?
1. M์ ๋์ง ์์ผ๋ฉด์ M์ ์ต๋ํ ๊ฐ๊น์ด ์นด๋๋ฅผ 3์ฅ์ ํฉ์ ์๋ ค๋ฉด ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ค ๋ฐ์ ธ๋ด์ผํ๋ค.
2. ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ค ๋ฐ์ ธ๋ณด๋ ์๊ณ ๋ฆฌ์ฆ ๊ธฐ๋ฒ์ ๋ถ๋ฅดํธํฌ์ค ์๊ณ ๋ฆฌ์ฆ์ด๋ผ๊ณ ํ๋๋ฐ, ๊ฑฐ์ฐฝํ๊ฑฐ ์์ด ๊ทธ๋ฅ ํ์ด์ฌ ๋ด์ฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
3. ์ด ๋ฌธ์ ์ ๊ฒฝ์ฐ, 3์ฅ์ "ํฉ"์ ๊ตฌํ๋ฏ๋ก ์นด๋์ ์์๋ ์ ๊ฒฝ์ฐ์ง ์์๋ ๋๋ค.
4. ๋ฐ๋ผ์ conbination(์กฐํฉ)์ผ๋ก ์นด๋ 3์ฅ์ ๊ตฌํ๋ฉด ๋๋ค.
๐ป์ฝ๋
from itertools import combinations
N,M = map(int,input().split())
card = list(map(int,input().split()))
temp = 0
card_com = list(map(sum,combinations(card,3)))
for i in card_com:
if i <= M and i > temp:
temp = i
print(temp)
โ ๊ฒฝ์ฐ์ ์ ( ์์ด / ์กฐํฉ / ์ค๋ณต ์์ด / ์ค๋ณต ์กฐํฉ)
- ์์ด : ์์๋ฅผ ๊ณ ๋ คํ์ฌ ๋ฝ๋ ๊ฒฝ์ฐ์ ์(์ค๋ณต x)
from itertools import permutations
a = [1,2,3]
print(list(permutations(a,2)))
#print
[(1,2),(1,3),(2,1),(2,3),(3,1),(3,2)]
- ์กฐํฉ : ์์๋ฅผ ๊ณ ๋ คํ์ง ์๊ณ ๋ฝ๋ ๊ฒฝ์ฐ์ ์(์ค๋ณต x)
from itertools import combinations
a = [1,2,3]
print(list(combinations(a,2)))
#print
[(1,2), (1,3), (2,3)]
- ์ค๋ณต ์์ด : ๋ฝ์ ์์๋ฅผ ๊ณ ๋ คํ๋, ์ฃผ๋จธ๋์ ๋ฝ์ ๊ตฌ์ฌ์ ๋ค์ ์ง์ด๋ฃ์ด ์ค๋ณต์ด ๊ฐ๋ฅํ๊ฒ ๋ฝ๋ ๊ฒฝ์ฐ์ ์(์ค๋ณต o)
from itertools import product
a = [1,2,3]
print(list(product(a,2)))
#print
[(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)]
- ์ค๋ณต ์กฐํฉ : ์์๋ฅผ ๊ณ ๋ คํ์ง ์๊ณ ์ฃผ๋จธ๋์ ๋ฝ์ ๊ตฌ์ฌ์ ๋ค์ ์ง์ด๋ฃ์ด ์ค๋ณต์ด ๊ฐ๋ฅํ๊ฒ ๊ตฌ์ฌ์ ๋ฝ๋ ๊ฒฝ์ฐ์ ์(์ค๋ณต o)
from itertools import combinations_with_replacement
a = [1,2,3]
print(list(combinations_with_replacement(a,2)))
#print
[(1,1),(1,2),(1,3),(2,2),(2,3),(3,3)]
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 7562 ๋์ดํธ์ ์ด๋ (ํ์ด์ฌ/python) (0) | 2023.07.11 |
---|---|
[๋ฐฑ์ค] 1697 ์จ๋ฐ๊ผญ์ง (ํ์ด์ฌ/python) (0) | 2023.06.20 |
[๋ฐฑ์ค] 5622 ๋ค์ด์ผ (ํ์ด์ฌ/python) (0) | 2023.06.19 |
[๋ฐฑ์ค] 1546 ํ๊ท (ํ์ด์ฌ/python) (0) | 2023.06.19 |
[๋ฐฑ์ค] 10818 ์ต์,์ต๋ (ํ์ด์ฌ/python) (0) | 2023.06.19 |