본문 바로가기
python-algorithm

백준 10989 수 정렬하기 3

by 무적김두칠 2022. 10. 11.

https://www.acmicpc.net/problem/10989

 

10989번: 수 정렬하기 3

첫째 줄에 수의 개수 N(1 ≤ N ≤ 10,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수가 주어진다. 이 수는 10,000보다 작거나 같은 자연수이다.

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
import sys
n=int(sys.stdin.readline())
ans=[0]*10001
for i in range(n):
    ans[int(sys.stdin.readline())]+=1
for i in range(10001):
    if ans[i]!=0:
        for j in range(ans[i]):
            print(i)
cs

 

문제 자체는.. 어렵지가 않은데
이 문제에서 내장함수 sort()를 쓰게되면 메모리 초과가 나오고
(This problem is not hard to just understand.. but If you use sort() function, You can see 'Memory Overflow')
수의 범위가 제한된 경우(문제에서는 10000이하)는 계수정렬(counting sort)를 사용하시면 됩니다
(When The numbers are limited(In this case under 10000), You can use counting sort )


반응형

'python-algorithm' 카테고리의 다른 글

백준 16466 콘서트  (0) 2022.10.13
백준 2204 도비의 난독증 테스트  (0) 2022.10.12
백준 25629 홀짝 수열  (0) 2022.10.10
백준 5217 쌍의 합  (0) 2022.10.09
백준 25377 빵  (0) 2022.10.08

댓글