본문 바로가기

전체 글

(31)
7/2 코테일지 (메모리 초과와 counting sort, array.index) 메모리 초과 Counting Sort 10989 수 정렬하기 3 다음 과 같은 문제인데, 들어오는 숫자의 수가 10,000,000개나 된다는 것을 알 수 있으며메모리 제한은 8MB이다. 정수 하나당 4B이므로 8MB/4B = 2MB= 2 *2^20= 약 2*10^6개의 정수를 받을 수 있다. 문제의 10^7개의 문자를 받을 수 없다.해당 사실을 모르고 초기에는 다음과 같이 작성을 하였고import sysn = int(sys.stdin.readline().rstrip())array = []for _ in range (n): array.append(int(sys.stdin.readline().rstrip()))array.sort()for element in array: print(element)메모리 초..
NeetCode 13번 알고리즘 : Counting Sort 니트코드 링크: https://neetcode.io/courses/dsa-for-beginners/13Bucket Sort라고 나와있는데 설명되어 있는 것은 사실상 Counting Sort이고 Bucket Sort는 조금 다른 알고리즘이다.Alogrithm Codedef bucketSort(arr): # Assuming arr only contains 0, 1 or 2 counts = [0, 0, 0] # Count the quantity of each val in arr for n in arr: counts[n] += 1 # Fill each bucket in the original array i = 0 for n in range(len(coun..
6/30 코테 일지 (deque) deque 사용법 2164 카드2deque는 doubly linked list로 구현되어 있어서 첫 번째 배열 pop시 array.pop(0)이 아닌 array.popleft()가 가능하며 연산속도가 굉장히 빠르다.import sys from collections import dequearray= deque()for i in range (int(sys.stdin.readline().rstrip())): array.append(i+1)while len(array) != 1: array.popleft() array.append(array.popleft())print(array[0]) 테스트로 밑에 같이 array.pop(0)으로 해보았을때 시간초과가 뜨는 것을 확인하였다. import sys from ..