์ ‘๊ทผ๋ฐฉ๋ฒ•) 

๊ธฐ๋‹ค๋ฆฐ ์‹œ๊ฐ„์˜ ํ•ฉ์ด ๊ฐ€์žฅ ์ž‘๊ฒŒ ๋‚˜์˜ฌ๋ ค๋ฉด ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„์ด ์ ์€ ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

๊ทธ ๋‹ค์Œ n๋ฒˆ์งธ ์‚ฌ๋žŒ ์†Œ์š”์‹œ๊ฐ„ + n-1๋ฒˆ์งธ ์‚ฌ๋žŒ ์†Œ์š”์‹œ๊ฐ„์˜ ํ•ฉ์„ ๋” ํ•ด์ค€ ๋‹ค์Œ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

 

์†Œ์Šค์ฝ”๋“œ

n = int(input())
array = list(map(int,input().split()))

array.sort()

   

for i in range(1,n):
    array[i] = array[i] + array[i-1]

print(sum(array))

์ ‘๊ทผ๋ฐฉ๋ฒ•

-๋ฐ”ํ…€ ์—… ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•˜์˜€๋‹ค.

 2 ,3 ์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ง€ ํ™•์ธ ํ•œ ํ›„์— ์ด๋ฅผ -1 ์„ ํ•œ ๊ฒฝ์šฐ์™€ ๋น„๊ตํ•ด ๋” ์ž‘์€ ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ์—ˆ๋””.

 

n= int(input())
d= [0] * (n+1)
d[1]=0



for i in range(2,n+1):
    if i%2 ==0 and i%3==0:
        d[i] = min(d[i//2]+1,d[i//3]+1)
    elif i%2 ==0 and i%3 !=0:
        d[i] = d[i//2]+1
    elif i%3 ==0 and i%2 !=0:
        d[i] = d[i//3]+1
    else:
        d[i] = d[i-1]+1
    d[i] = min(d[i-1]+1,d[i])
    print(d[i])
   
       
print(d[n])
 
์ ‘๊ทผ๋ฐฉ์‹
- ์ตœ์†Œ ๋ด‰์ง€์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 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])
   

+ Recent posts