๊ด€๋ฆฌ ๋ฉ”๋‰ด

Tech Log ๐Ÿ› ๏ธ

๋ฐฑ์ค€ 2839) ์„คํƒ•๋ฐฐ๋‹ฌ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜/๊ทธ๋ฆฌ๋””

๋ฐฑ์ค€ 2839) ์„คํƒ•๋ฐฐ๋‹ฌ

sehaan 2022. 2. 8. 20:54
 
์ ‘๊ทผ๋ฐฉ์‹
- ์ตœ์†Œ ๋ด‰์ง€์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 3๋ณด๋‹จ 5๋กœ ๋งŽ์ด ๋‚˜๋ˆ„์–ด์•ผํ•œ๋‹ค.
  ๋”ฐ๋ผ์„œ ํ‚ฌ๋กœ๊ทธ๋žจ์—์„œ 3์”ฉ ๋งˆ์ด๋„ˆ์Šค๋ฅผ ํ•ด์ฃผ์–ด 5๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š”์ง€ ํ™•์ธํ•˜์˜€๋‹ค.
  3์„ ๊ณ„์† ๋นผ์ฃผ์–ด n์ด ๋งˆ์ด๋„ˆ์Šค๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ๋Š” -1์„ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.
 
 
 
n = int(input())
cnt =0

while n >0:
    if n%5 == 0:
        cnt += n//5
        break
    n -= 3
    cnt +=1

if n >=0:
    print(cnt)
else:
    print(-1)
 
 
ํ’€์ด ์ค‘์— dp ๋ฐฉ์‹์œผ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™์•„์„œ ์ฝ”๋”ฉ์„ ํ•ด๋ณด์•˜๋‹ค.
 
-------------
n = int(input())

d = [10001] *(n+1)
cnt = 0

d[0] = 0
d[3] = 1
for i in range(5,n+1):
    d[i] = min(d[i-3],d[i-5]) + 1

if d[n] == 10001:
    print(-1)
else:
    print(d[n])