๐ ํ์์ ์๊ณ ๋ฆฌ์ฆ Greedy Algorithm
ํ์์ ์๊ณ ๋ฆฌ์ฆ(Greedy Algorithm)์ ๋์ ํ๋ก๊ทธ๋๋ฐ(Dynamic Programming) ์๊ธฐ๋ฅผ ๋นผ๋๊ณ ์๋ ํ ์ ์๋ค. ๋์ ํ๋ก๊ทธ๋๋ฐ์ ๋์ค์ ๋ ํ๊ฒ ์ง๋ง ๊ฐ๋จํ ์ค๋ช ํ์๋ฉด ์ ์ฒด ๋ฌธ์ ๋ฅผ ์ฌ๋ฌ ๊ฐ์ ํ์ ๋ฌธ์ ๋ก ๋๋์ด ํ๊ณ , ํ์ ๋ฌธ์ ๋ค์ ํด๊ฒฐ๋ฐฉ๋ฒ์ ๊ฒฐํฉํด ์ต์ข ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ด๋ค. ์ฆ, ๋์ ํ๋ก๊ทธ๋๋ฐ์ ์ฌ์ค์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ชจ๋ ๋ฐฉ๋ฒ์ ํ์ํ๋ฏ๋ก ์ ํํ์ง๋ง ์๋๊ฐ ๋๋ฆด ์ ๋ฐ์ ์๋ค.
๋์ ํ๋ก๊ทธ๋๋ฐ์ ๋๋ฆฐ ์๋๋ฅผ ๋ณด์ํ๊ณ ์ ๋ฑ์ฅํ ๊ฐ๋ ์ด ํ์์ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. ํ์์ (Greedy), ๋ง ๊ทธ๋๋ก ํ์ฌ ์ํฉ์์ ๋น์ฅ ์ข์ ๊ฒ๋ง์ ๊ณ ๋ฅด๋ ๋ฐฉ๋ฒ์ด๋ค. ๊ทธ ์๊ฐ์ ์ต์ ์ด๋ผ๊ณ ์๊ฐํ๋ ๊ฒฐ์ ์ ํ๋ค. (๋ฏธ๋๋ฅผ ์ ํ ๊ณ ๋ คํ์ง ์์) ๋๋ถ์ ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ง์ญ ์ต์ ํด(local optima) ๊ฐ๋ ๊ณผ ๊ฐ์ด ์ฐ์ธ๋ค. ํ์ง๋ง ๋น ๋ฅด๊ฒ ๊ฒฐ์ ํ ์ ์๋ค๋ ์ฅ์ ์ด ์๋ค.
์ด์จ๊ฑฐ๋ ํ์์ ์๊ณ ๋ฆฌ์ฆ๊ณผ ๋์ ํ๋ก๊ทธ๋๋ฐ(DP)์ ๋ฐ๋๋๋ ๊ฐ๋ ์ด์ง๋ง ์๋ก ๋ณด์ํ๋ ๊ด๊ณ์ ์๋ค.
๊ทธ๋ฆฌ๋ ์ ํ์ ๊ฐ์ฅ ๋ํ์ ์ธ ๊ฑฐ์ค๋ฆ๋ ๊ณ์ฐ ๋ฌธ์ ๋ฅผ ์๊ฐํด๋ณด์. ๊ฑฐ์ฌ๋ฌ ์ค์ผ ํ ๋์ ์ ์ต์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ๋ก, ๊ฐ์ฅ ํฐ ํํ ๋จ์๋ถํฐ ๋์ ๊ฑฐ์ฌ๋ฌ ์ค๋ค๋ ์์ด๋์ด๊ฐ ํต์ฌ์ด๋ค.
n = 1260
count = 0
# ํฐ ๋จ์ ํํ๋ถํฐ ์ฐจ๋ก๋๋ก
coin_types = [500, 100, 50, 10]
for coin in coin_types:
count += n // coin
n %= coin
print(count)
์๊ฐ๋ณต์ก๋๋ ํํ์ ์ข ๋ฅ๊ฐ K๊ฐ ์ผ ๋ K๋ฒ๋งํผ ๋ฐ๋ณตํด์ผ ํ๋ฏ๋ก O(K)์ด๋ค.
๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ์ด ๋ชจ๋ ๋ฌธ์ ์ ์ ์ฉํ ์ ์๋ ๊ฑด ์๋๋ฏ๋ก ์ด ์๊ณ ๋ฆฌ์ฆ์ ์ ๋น์ฑ์ ๋ํด ๊ณ ๋ฏผํด๋ด์ผ ํ๋ค. ๊ฑฐ์ค๋ฆ๋ ๋ฌธ์ ์์ ๋์ ์ ๋จ์๊ฐ ์๋ก ๋ฐฐ์ ํํ๊ฐ ์๋๋ผ ๋ฌด์์๋ก ์ฃผ์ด์ง ๊ฒฝ์ฐ์๋ ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํด๊ฒฐํ ์ ์๋ค. ์ด๋ฐ ๊ฒฝ์ฐ์๋ ์์์ ๋งํ๋ฏ ๋์ ํ๋ก๊ทธ๋๋ฐ(DP) ๊ธฐ๋ฒ์ ์ฌ์ฉํด์ผ ํ๋ค.
[ ํฐ ์์ ๋ฒ์น ]
๋ค์ํ ์๋ก ์ด๋ฃจ์ด์ง ๋ฐฐ์ด์ด ์์ ๋ ์ฃผ์ด์ง ์๋ค์ M๋ฒ ๋ํด ๊ฐ์ฅ ํฐ ์๋ฅผ ๋ง๋๋ ๋ฒ์น์ด๋ค.
๊ฐ์ฅ ํฐ ์๋ฅผ K๋ฒ, ๋๋ฒ์งธ๋ก ํฐ ์๋ฅผ 1๋ฒ ๋ํ๋ ์ฐ์ฐ์ ๋ฐ๋ณตํ๋ฉด ๋๋ ๊ฐ๋จํ ๋ฌธ์ ์๋ค.
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
first = arr[-1]
second = arr[-2]
answer = first * k * (m // k) + second * (m % k)
print(answer)
[ ์ซ์ ์นด๋ ๊ฒ์ ]
N * M ํํ์ ์ซ์ ์นด๋ ๋ค ์ค ํ์ ๋จผ์ ์ ํํ๊ณ , ๊ทธ ํ์ ํฌํจ๋ ์นด๋๋ค ์ค ๊ฐ์ฅ ์ซ์๊ฐ ๋ฎ์ ์นด๋๋ฅผ ๋ฝ๋๋ค๊ณ ํ ๋, ์ต์ข ์ ์ผ๋ก ๊ฐ์ฅ ๋์ ์ซ์์ ์นด๋๋ฅผ ๋ฝ์ ์ ์๋๋ก ํด์ผํ๋ค. ์ฆ, ๊ฐ ํ๋ง๋ค ๊ฐ์ฅ ์์ ์๋ฅผ ์ฐพ๊ณ ๊ทธ ์๋ค ์ค ๊ฐ์ฅ ํฐ ์๋ฅผ ์ฐพ์ผ๋ฉด ๋๋ค.
n, m = map(int, input().split())
answer = 0
for _ in range(n):
data = list(map(int, input().split()))
answer = max(answer, min(data))
print(answer)
[ 1์ด ๋ ๋๊น์ง ]
์ด๋ค ์ N์ด 1์ด ๋ ๋๊น์ง ๋ค์์ ๋ ์ฐ์ฐ ์ค ํ๋๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ์ ํํด ์ํํ๋ค. (๋๋ฒ์งธ ์ฐ์ฐ์ N์ด K๋ก ๋๋์ด ๋จ์ด์ง ๋๋ง)
1. N์์ 1์ ๋บ๋ค
2. N์ K๋ก ๋๋๋ค
# sol1: N์ด K์ ๋ฐฐ์๊ฐ ๋ ๋๊น์ง ๋ฐ๋ณตํด 1์ ๋นผ๊ฑฐ๋, K์ ๋ฐฐ์๊ฐ ๋๋ฉด K๋ก ๋๋๊ธฐ
n, k = map(int, input().split())
count = 0
while n > 0:
if n == 1:
break
if n % k:
n -= 1
else:
n //= k
count += 1
print(count)
# sol2: N์ด K์ ๋ฐฐ์๊ฐ ๋ ๋๊น์ง์ ์๋ฅผ ํ๋ฒ์ ๋นผ๋ ๊ฒ (๋ฐ๋ณต๋ฌธ ์คํํ์๋ฅผ ์ค์ฌ์ค)
n, k = map(int, input().split())
count = 0
while n > 0:
if n == 1:
break
target = n % k
count += target
n -= target
n //= k
count += 1
print(count)
ํ์์ ์๊ณ ๋ฆฌ์ฆ์ ํ๋์ ํ๋ฌธ์ (TSP), ๋ถํ ๊ฐ๋ฅํ ๋ฐฐ๋ญ ๋ฌธ์ , ๊ฑฐ์ค๋ฆ๋ ๋ฌธ์ ๋ฑ์์ ์ฐ์ด๋๋ฐ ์์๋ ๋ช ๋ฒ ๋ดค์๊ธฐ ๋๋ฌธ์ ์คํตํ๊ณ ๋ฐ๋ก ๋ฐฑ์ค ๋ฌธ์ ๋ฅผ ํ์๋ค. ์คํฐ๋์์ ํ๊ธฐ๋ก ํ ๋ฐฑ์ค 4๋ฌธ์ ๋ฅผ ํ๊ณ ๊ตฌ๊ธ๋งํ๋ค ์ฐพ์ ๋ฐฑ์ค ๋ฌธ์ ๋ชจ์์ ์ฐจ๊ทผ์ฐจ๊ทผ ํ์ด๋ณด๋ คํ๋ค. ๋ฌธ์ ํผ ๊ฑด ์๋ ๋งํฌ๋ก ์ฒจ๋ถํด๋๊ฒ ๋ค. (ํ๋ ๊ธ์จ ํด๋ฆญํ๋ฉด ๋งํฌ๋ก ์ด๋)
๊ทธ๋ฆฌ๋ ๋ฌธ์ ํ์ด (1) : C++
- 11047๋ฒ : ๋์ 0
- 4796๋ฒ : ์บ ํ
- 13305๋ฒ : ์ฃผ์ ์
- 16435๋ฒ : ์ค๋ค์ดํฌ๋ฒ๋
๊ทธ๋ฆฌ๋ ๋ฌธ์ ํ์ด (2) : C++
- 11399๋ฒ : ATM
- 2217๋ฒ : ๋กํ
- 13458๋ฒ : ์ํ๊ฐ๋
๊ทธ๋ฆฌ๋ ๋ฌธ์ ํ์ด (3) : Python
์ด์ฝํ ์ฑํฐ3 ๊ทธ๋ฆฌ๋ ํํธ ์์ +์ฐ์ต๋ฌธ์ ํ์ด
- ๋ง๋ค ์ ์๋ ๊ธ์ก (K ๋ํ ๊ธฐ์ถ)
- ๋ณผ๋ง๊ณต ๊ณ ๋ฅด๊ธฐ (2019 SW ๋ง์์คํธ๋ก ์ ํ ํ ์คํธ)
- ๋ฌด์ง์ ๋จน๋ฐฉ ๋ผ์ด๋ธ (2019 ์นด์นด์ค ์ ์ ๊ณต์ฑ)
๐ ๋์ค์ ๋ค์ ๋ณผ๋งํ ๋งํฌ๋ชจ์
์ด ๊ธ์ '์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค with ํ์ด์ฌ(๋๋๋น)' ์ฑ ๋ด์ฉ๊ณผ ์๋ก ๋ฌธ์ ๋ฅผ ์ฐธ๊ณ ํ์ต๋๋ค. (๋ฌธ์ ๊ฐ ๋๋ค๋ฉด ์ญ์ ํ๊ฒ ์ต๋๋ค!)