본문 바로가기

알고리즘/구현

(29)
파이썬 | 백준 | 1173 | 운동 solution 시키는 대로 하면 된다.. # 1173, 운동 import sys N, m, M, T, R = map(int, sys.stdin.readline().split()) sec, sec_exercise = 0, 0 # 총 필요한 시간, 운동한 시간 now = m if now > M or now + T > M: # 운동을 할 수 없으면 print(-1) sys.exit() while sec_exercise < N: if now + T M: now -= R if now < m: # X-R이 m보다 작으면 맥박은 m이다. now = m sec += 1 print(sec) 문제 출처 www.acmicpc.net/problem/1173 1173번: 운동 첫째 줄에 다섯 정수 N, m, M, T, R이 ..
파이썬 | 백준 | 16918 | 봄버맨 solution 3 : 폭탄이 설치되어 있지 않은 모든 칸에 폭탄을 설치한다 → make_bombs() 4 : 1초가 지난 후에 3초 전에 설치된 폭탄이 모두 폭발한다 → explode() # 16918, 봄버맨 import sys from collections import deque def loc_bombs(): # 폭탄 위치 찾아 bombs deque에 저장 for i in range(R): for j in range(C): if board[i][j] == 'O': bombs.append((i, j)) def make_bombs(): # 모든 자리에 폭탄 설치 for i in range(R): for j in range(C): if board[i][j] == '.': board[i][j] = 'O' ..
파이썬 | 백준 | 1744 | 수 묶기 solution 1. 수가 양수인 경우, 음수인 경우를 나눈다. 양수 → 최대힙 음수 → 최소힙 2. 우선순위 큐 사용 # 1744, 수 묶기 import sys import heapq N = int(sys.stdin.readline()) positive = [] negative = [] zero = False for _ in range(N): num = int(sys.stdin.readline()) if num > 0: heapq.heappush(positive, (-num, num)) # 양수 elif num < 0: heapq.heappush(negative, num) # 음수 else: zero = True # 0이 있는지 res = 0 # 양수 flag = 0 tmp1, tmp2 = 0, 0 w..
파이썬 | 백준 | 3048 | 개미 solution 방향 다르면 교환 # 3048, 개미 import sys N1, N2 = map(int, sys.stdin.readline().split()) ants1 = list(sys.stdin.readline().rstrip()) ants2 = list(sys.stdin.readline().rstrip()) T = int(sys.stdin.readline()) dir = {}# 방향 저장 for ant in ants1: dir[ant] = 0 # 뒤로 이동 for ant in ants2: dir[ant] = 1 # 앞으로 이동 ants1.reverse() ants1.extend(ants2) for _ in range(T): i = 0 while i < len(ants1)-1: if dir[ants..
파이썬 | 백준 | 1713 | 후보 추천하기 | defaultdict(int) solution 1. 우선순위 큐 이용 (추천 횟수, 몇 번째 추천인지, 후보 번호) --> 안 돼서 다시 해봐야함 # 1713, 후보 추천하기 import sys import heapq # 횟수, 오래, 학생 n = int(sys.stdin.readline()) k = int(sys.stdin.readline()) recommend = list(map(int, sys.stdin.readline().split())) heap = [] candidate = {} for i in range(len(recommend)): # 사진에 없는 사람 추천 받은 경우 if candidate.get(recommend[i]) is None: # 사진이 꽉차있으면 1) 횟수 비교 2) 최근에 추천받은지 비교 if len(c..
파이썬 | 백준 | 2217 | 로프 solution 1. 내림차순 2. 15, 10의 경우 1. 15로프를 이용해 15의 중량 들어올리는 경우 2. 15로프, 10로프를 이용해 20의 중량을 10/10으로 들어올리거나 # 2217, 로프 import sys import heapq n = int(sys.stdin.readline()) heap = [] for _ in range(n): w = int(sys.stdin.readline()) heapq.heappush(heap, (-w, w)) r = 1 max_ = 0 while heap: max_ = max(max_, r * heapq.heappop(heap)[1]) r += 1 print(max_) 문제 출처 www.acmicpc.net/problem/2217 2217번: 로프 N(1≤N≤..
파이썬 | 백준 | 17822 | 원판 돌리기 solution 1. bfs 이용해 같은 수가 인접해있으면 0으로 바꿈 20%대에서 틀림 반례 찾아봐야 함 # 17822, 원판 돌리기 import sys from collections import deque dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def bfs(x, y, n): global flag q = deque() q.append((x, y)) visited[x][y] = 1 near = False while q: a, b = q.popleft() if a != 0: nx = a-1 if circle[nx][b] == n and visited[nx][b] == 0: near = True visited[nx][b] = visited[a][b]+1 q.append((nx,..
파이썬 | 프로그래머스 | 숫자 게임 solution 1 : 배열 이용 → 시간 초과 def solution(A, B): A = sorted(A, reverse=True) B = sorted(B, reverse=True) score = 0 i, j = 0, 0 while i -n : 우선순위 import heapq def solution(A, B): heapA = [] heapB = [] for i in range(len(A)): heapq.heappush(heapA, (-A[i], A[i])) hea..