목록BaekJoon (11)
Hack Your World
import sys from collections import deque input = sys.stdin.readline n, k = map(int, input().split()) # n: 플러그 개수 usage = list(map(int, input().split())) plug = usage[:1] # 앞 두개가 모두 같은 숫자일 수 있으므로 1개만 꽂아넣기 usage = deque(usage[1:]) result = 0 while usage: i = usage.popleft() # 이미 꽂혀있을 경우 if i in plug: continue # 플러그 자리가 남았을 경우 if len(plug) < n: plug.append(i) continue # 꽂혀있는 것들 중 뽑을 것 고르기(plug inde..

import sys from collections import deque input = sys.stdin.readline q = deque() for _ in range(int(input())): command = input().rstrip() if command == "pop": print(-1 if len(q) == 0 else q.popleft()) elif command == "size": print(len(q)) elif command == "empty": print(+(not q)) elif command == "front": print(-1 if len(q) == 0 else q[0]) elif command == "back": print(-1 if len(q) == 0 else q[-1])..
import sys input = sys.stdin.readline # 팀의 수가 최소 # i번은 팀원 수가 자기 자신 포함 xi명 이하여야함 n = int(input()) xi = [0 for _ in range(n+1)] # xi의 범위는 1 O(n) 다른 사람 풀이 - 위 아이디어를 좀 더 간단하게 구현할 수 있다. xi 배열을 인원수 측정에 사용하지 않고 그냥 입력값 그대로 받아서 오름차순으로 정렬한다. 그리고 그냥 최대 인원 수 대로 끊어서 센다(간단 그잡채ㅋㅋㅋㅋ내 고민이 너무 허무해~) N = int(input()) X = list(map(int,input().split())) X.sort() i, cnt = 0, 0 while i < len(X): cnt += 1 i += X[i] prin..
import sys from collections import deque input = sys.stdin.readline n = int(input()) q = deque([i for i in range(1, n+1)]) while len(q)!=1: q.popleft() q.append(q.popleft()) print(q[0]) 큐로 풀면 되는 아주 간단한 문제다 시간 복잡도 deque는 double-linked list로 구현되어 있음 그래서 양 끝의 요소의 추가/삭제가 O(1)을 만족 반면, Python의 list는 fixed size memory blocks(array)로 구현되어 있음. 링크드 리스트처럼 보이지만 고정된 사이즈의 메모리를 갖는 array 따라서 리스트의 마지막 원소 삭제는 O(1..
import sys input = sys.stdin.readline DIV = 1000000009 dp = [1, 2, 4, 7] for _ in range(int(input())): # 방법의 총 수 dp, 순서 바꾸는 것도 포함이네.. n = int(input()) for i in range(len(dp), n): # 알아서 1, 2, 3이 각각 들어갈 것 -> 겹치는게 없음 dp.append((dp[i-1] + dp[i-2] + dp[i-3]) % DIV) print(dp[n-1]) 점화식은 발견 했는데 시간 초과, 메모리 초과를 어떻게 해결해야할지 생각해내는게 어려웠다. 이를 해결한 방법은 다음과 같다. 1. 모든 테스트케이스는 같은 DP 값을 가지니 DP를 global 하게 사용 2. 이미 있는..