[Baekjoon] 1655. 가운데를 말해요
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/1655❔ThinkingN개의 수가 차례대로 주어질 때, 현재까지 숫자들의 중앙값을 출력한다.각 숫자는 -10,000이상 10,000이하의 정수이다.💻Solutionimport sysimport heapqinput = sys.stdin.readlinemax_heap = []min_heap = []N = int(input().rstrip())for _ in range(N): num = int(input().rstrip()) heapq.heappush(max_heap, -num) if max_heap and min_heap and (-max_heap[0] > min_heap[0]): heapq...
[Baekjoon] 2225. 합분해
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/2225❔Thinking0부터 N까지의 숫자를 중복하여 K개 뽑아서, 합이 N이 되는 경우의 수를 반환한다.계산의 순서가 다르면 다른 경우로 생각한다. (ex - 1+2, 2+1은 다른 경우)💻SolutionN, K = map(int, input().split())a,b = 1,1for i in range(1, K): a *= N + i b *= iprint((a // b) % 1000000000)🗝️keypoint합이 N이어야 하기 때문에, 공 N개를 K-1개의 칸막이로 구분짓는 경우와 같다고 생각할 수 있다.(n-1+r)! // (n-1)! * (r)!  인 중복 조합의 공식에 대입하면 (N+K-1)! ..
[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..
[Baekjoon] 2565. 전깃줄
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/2565❔Thinking전봇대 A와 B를 연결하는 전깃줄이 겹치지 않도록 몇개를 제거할 때, 최소한의 개수를 반환한다.💻Solutionimport sysfrom bisect import bisect_leftinput = sys.stdin.readlineN = int(input().rstrip())left = []for _ in range(N): A, B = map(int, input().split()) left.append([A,B])left.sort()def cross_check(line_list:list): prev = [line_list[0][1]] for a, b in line_list: ..
[Baekjoon] 2573. 빙산
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/2573❔Thinking2차원 배열에 빙산의 높이가 주어질 때, 일년마다 상하좌우에 있는 0의 개수만큼 높이가 줄어든다한 덩어리의 빙산이 두개 이상의 덩어리가 되는 최소한의 년수를 반환한다.상하좌우로 붙어 있어야 한 덩어리에 속한다. (즉, 대각선을 포함되지 않는다.)💻Solutionimport sysfrom collections import dequeinput = sys.stdin.readlineN, M = map(int, input().split())board = [list(map(int, input().split())) for _ in range(N)]total_ice = sum(cell > 0 for row in..
[Baekjoon] 1062. 가르침
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/1062❔Thinking정해진 개수의 알파벳 조합을 만들 때, 해당 알파벳들로 만들 수 있는 단어의 최대 개수를 반환한다.anta, tica가 앞 뒤로 붙기 때문에, 총 5개의 알파벳은 조합에 무조건 포함되어야 한다. 💻Solutionimport sysfrom itertools import combinationsinput = sys.stdin.readlineN, K = map(int, input().split())words = []for _ in range(N): word = input().rstrip() words.append(set(word[4:-4]))if K 🗝️keypoint주어진 단어들에 등장하는..