본문 바로가기
728x90

Programming/Python46

[프로그래머스] K번째 수 기본아이디어하라는대로 구현한다def solution(array, commands): answer = [] for i, j, x in commands: answer.append(sorted(array[i-1:j])[x-1]) return answer딱히 할 말이 없다.... 2025. 4. 2.
[프로그래머스] 체육복 기본 아이디어for문 여러번 돌리기보다 index로 접근하는 것이 좋겠다.무식하게 해도 될거같은데...?1. 인덱스 접근def solution(n, lost, reserve): status = [1]*n # all students for a in lost: # losted students status[a-1] -= 1 for b in reserve: status[b-1] += 1 for i in range(n): # 내가 체육복 여벌 있을 때 if status[i] == 2: if i > 0 and status[i-1] == 0: status[i] -= 1 .. 2025. 3. 27.
[프로그래머스] 최소직사각형 기본아이디어한 변의 최대값 다른 한 변의 최대값 구해서 곱하기명함은 돌릴 수 있으니 미리 돌려놓고 계산하기def solution(sizes): row = 0 col = 0 for w, h in sizes: if w 좌(큰 변) / 우(작은 변)으로 몰아놓기: 가로세로 치환 2025. 3. 27.
[프로그래머스] 같은 숫자는 싫어 기본아이디어요소 하나씩 보면서 전꺼랑 다르면 새 리스트에 appenddef solution(arr): answer = [arr[0]] for i in range(1, len(arr)): if arr[i] != arr[i-1]: answer.append(arr[i]) return answer첨에 collections.deque로 arr를 감싼담에, 큐에서 leftpop()해가면서 전꺼랑 다르면 answer에 appned()했는데그러면 리스트를 덱으로 감싸는 비용 O(n)이 드니까. 굳이굳이 그럴 필요없음리스트의 left요소 삽입 / 제거를 반복적으로 해야한다 O(n^2) -> 덱으로 감싸기. popleft()는 O(1)리스트의 left부터 요소를 탐색해야한다.. 2025. 3. 26.
[프로그래머스] 완주하지 못한 선수 기본 아이디어participant - completion주의 사항'참가자 중에는 동명이인이 있을 수 있다'from collections import Counterdef solution(participant, completion): answer = Counter(participant) - Counter(completion) return list(answer.keys())[0]동명이인을 고려해야하기 때문에 set(participant) - set(completion)을 하면 안 됨. (동명이인이 집계되지 않음)동명이인도 개별키로 고려하여 집계하기 위해 collections.Counter클래스 사용answer.keys()는 딕셔너리 뷰이므로 인덱스 접근 불가함. list()처리 후 첫번째 인덱스 접.. 2025. 3. 26.
[백준] 2164 | 카드2 뭔가 속도 차가 확실히 나니까 작고 소중한 코드 고치기 재밌다...히히... 4년 전 코드 import sysimport collectionsinput = sys.stdin.readlineN = int(input())deque = collections.deque([i for i in range(1, N+1)])while len(deque) > 1: deque.popleft() deque.rotate(-1)print(deque[0]) (good) sys.stdin 써서 입력 관리(bad) rotate 저건 왜 쓴거 굳이 -> 모든 요소를 한 칸씩 이동시킴 (오버스펙;;:)개선 코드from collections import dequeimport sysinput = sys.stdin.readline.. 2024. 12. 11.
728x90