Queue-ri/Advanced-Algorithm-Study

[Week 1] STRJOIN self review - ChaeheeKang-GitHub

Closed this issue · 0 comments

STRJOIN self review

  • 파일명: STU/STRJOIN/ChaeheeKang-Github.py
  • 수행시간: 64 ms

1. 고민 과정

리스트를 정렬해서 작은 수끼리 더하면 되겠다고 생각했는데 pop을 이용하기 위해선 작은수가 뒤로 가야한다고 생각했습니다.
.

2. 풀이 아이디어

역정렬을 통해 마지막 수와 마지막 전의 수를 더해 변수 s에 저장하고 pop을 통해 빼낸 후, 새로운 수인 s를 기존의 list에 append하여 문제를 해결하였습니다.

3. 작성한 코드와 설명

def strjoin(l):
    cnt=0
    while (len(l)>2):
        s=0
        l.sort(reverse=True)
        s=l[-1]+l[-2]
        cnt+=s
        l.pop()
        l.pop()
        l.append(s)
    cnt+=l[0]+l[1]
    print(cnt)
    
c=int(input())
for _ in range(c):
    n=int(input())
    l=list(map(int,input().split()))
      
    strjoin(l)