백준 / 1107번 / 리모컨 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/1107  코드 import sysinput = sys.stdin.readlinen = int(input())m = int(input())ans = abs(100 - n)if m: btn = list(input().split())else: btn = []for num in range(1000001): for i in str(num): if i in btn: break else: ans = min(ans, len(str(num)) + abs(num - n))print(ans) 처음에는 버튼을 최소한으로 눌러야 한다는 조건에 고장난 버튼의 최소, 최댓값과 ans(n - 100)..
백준 / 28278번 / 스택 2 / python 파이썬
·
코딩테스트/백준 (python)
문제 : 28278번: 스택 2 (acmicpc.net) https://www.acmicpc.net/problem/1181  기초적인 스택의 동작 방식만 알면 쉽게 풀 수 있는 문제였다. 코드 import sysinput = sys.stdin.readlinestack = []n = int(input())for i in range(n): x = list(map(int, input().split())) if x[0] == 1: stack.append(x[1]) elif x[0] == 2 : if stack: print(stack.pop()) else: print(-1) elif x[0] == 3: ..
백준 / 1629번 / 곱셈 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/1629  처음 문제를 보고 쉬운 문제라고 생각하고 바로 풀었으나 import sysinput = sys.stdin.readlinea, b, c = map(int, input().split())print((a ** b) % c) 바로 시간 초과가 났다. 이 문제는 Divide and Conquer, 분할 정복의 원리를 사용해야 풀 수 있는 문제였다.  분할 정복이란?분할 정복 (Divide and Conquer)은 둘 이상의 부분 문제로 나눈 뒤 각 문제에 대한 답을 재귀 호출을 이용해 계산하고, 각 부분 문제의 답으로부터 전체 문제의 답을 계산한다.  분할 정복이 일반 재귀 호출과 다른 점은 문제를 한 조각과 전체로 나누는 대신, 비슷한..
백준 / 2346번 / 풍선 터뜨리기 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/2346   코드from collections import dequeimport sysinput = sys.stdin.readlinen = int(input())q = deque(enumerate(map(int, input().split())))ans =[]while q: idx, num = q.popleft() ans.append(idx + 1) if num > 0: q.rotate(-(num - 1)) elif num   deque.rotate() deque는 rotate() 함수를 사용하여 회전시킬 수 있다.- rotate(1): 인자 값이 양수이면 오른쪽 회전- rotate(-1): 인자 값이 음수..
백준 / 7576번 / 토마토 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/7576  코드import sysfrom collections import dequeinput = sys.stdin.readlinem, n = map(int, input().split())tomato = []queue = deque()dx = [-1, 1, 0 ,0]dy = [0, 0, -1, 1]for _ in range(n): tomato.append(list(map(int, input().split())))for i in range(n): for j in range(m): if tomato[i][j] == 1: queue.append([i, j])def BFS(): while queue..
백준 / 1463번 / 1로 만들기 / DP / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/1463  코드 import sysn = int(sys.stdin.readline())d = [0] * (n + 1)for i in range(2, n + 1): d[i] = d[i-1] + 1 if i % 3 == 0: d[i] = min(d[i], d[i//3] + 1) if i % 2 == 0: d[i] = min(d[i], d[i//2] + 1)print(d[n]) DP를 이용해 모든 숫자에 대해 /3, /2, -1을 하는 함수를 호출할 필요없이 이전에 계산한 정보들을 사용하여 푸는 것이 핵심이다. DP 테이블에 해당 숫자를 만드는데 필요한 최소 연산 횟수를 갱신해가며 저장하면 d[n]에는 ..
백준 / 2740번 / 행렬 곱셈 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/2740  코드N, M = map(int, input().split())matA = []matB = []for _ in range(N): matA.append(list(map(int, input().split())))M, K = map(int, input().split())for _ in range(M): matB.append(list(map(int, input().split())))matrix = [[0 for _ in range(K)] for _ in range(N)]for n in range(N): for k in range(K): for m in range(M): matrix[n][k..
백준 / 1260번 / DFS와 BFS / DFS, BFS / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/1260  코드 from collections import dequeimport sysinput = sys.stdin.readlinen, m, v = map(int, input().split())graph = [[] for _ in range(n+1)]for _ in range(m): a, b = map(int, input().split()) graph[a].append(b) graph[b].append(a)def DFS(start): visited[start] = True print(start, end = " ") for i in graph[start]: if not visited[i]: ..