[Beakjoon] 1922. 네트워크 연결
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/1922❔Thinking모든 컴퓨터가 연결되어야 할 때, 최소한의 비용을 반환한다.각 컴퓨터를 연결하는 비용이 주어진다.💻Solutionimport sysimport heapqinput = sys.stdin.readlineN = int(input().rstrip())M = int(input().rstrip())lines = []for _ in range(M): a,b,c = map(int, input().split()) heapq.heappush(lines, (c,a,b))def find_parent(x, parent): if x != parent[x]: parent[x] = find_par..
[Beakjoon] 4195. 친구 네트워크
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/4195❔Thinking친구 관계가 주어지면, 해당 친구 네트워크에 속한 인원 수를 반환한다.친구 네트워크는, root가 같은 사람들을 뜻한다.💻Solutionimport sysinput = sys.stdin.readlinedef find_parent(x, parent): if x != parent[x]: parent[x] = find_parent(parent[x], parent) return parent[x]def union(a, b, parent, size): a = find_parent(a, parent) b = find_parent(b, parent) if a != b: ..
[Beakjoon] 20040. 사이클 게임
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/20040❔Thinking방향이 없는 그래프에서 사이클이 형성되었는지 확인하고, 그 순간을 출력한다.사이클이 없다면 0을 출력한다.💻Solutionimport sysinput = sys.stdin.readlineN, M = map(int, input().split())roots = [i for i in range(N)]def find_root(x): if x != roots[x]: roots[x] = find_root(roots[x]) return roots[x]def union(a, b, roots): a = find_root(a) b = find_root(b) roots[max..
[Beakjoon] 1213. 펠린드롬 만들기
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/1213❔Thinking주어진 문자열을 사전순으로 앞서는 '펠린드롬' 문자열을 만들어 반환한다.똑바로해도, 거꾸로해도 같아야 하기 때문에, 배열 세개로 해결한다. (left, mid, right)만약 가운데 오는 문자열이 2개 이상일 경우, 펠린드롬 문자열을 만들 수 없다. (ex - AAABBB)💻Solutionname = list(input())name.sort()char_set = set()left, mid, right = [], [], []for char in name: if char not in char_set: char_set.add(char) tmp = name.count(ch..
[Beakjoon] 7579. 앱
·
코딩테스트
🔗 Problem Linkhttps://www.acmicpc.net/problem/7579❔Thinking최소한의 비용으로 M 메모리 만큼을 확보해야 한다 => M 메모리가 확보되었을 경우 중, 최소 비용을 찾는다60의 메모리를 3+3과 0+4로 만들 수 있기 때문에, 무조건 적은 비용으로 계산해나가는 방법을 사용할 수 없다.💻Solutionimport sysinput = sys.stdin.readlineN, M = map(int, input().split())Ms = list(map(int, input().split()))Cs = list(map(int, input().split()))datas = [[m,c] for m,c in zip(Ms, Cs)]answer = 10001volume = sum..
[Beakjoon] 7785번 - 회사에 있는 사람
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/7785❔Thinking주어진 N개의 회사 출퇴근 log를 통해, 현재 회사에 남아 있는 사람의 이름을 출력한다.대소문자를 구분하여 다른 이름으로 취급한다.결과는 사전순의 역순으로 출력한다.💻Solutionimport sysinput = sys.stdin.readlinen = int(input().rstrip())member = {}for _ in range(n): name, state = input().split() if state == 'enter': member[name] = 1 elif state == 'leave': del member[name]now_in_company..