Python 234

[백준 14719번] 빗물

14719번: 빗물 첫 번째 줄에는 2차원 세계의 세로 길이 H과 2차원 세계의 가로 길이 W가 주어진다. (1 ≤ H, W ≤ 500) 두 번째 줄에는 블록이 쌓인 높이를 의미하는 0이상 H이하의 정수가 2차원 세계의 맨 왼쪽 위치 www.acmicpc.net from collections import deque h, w = map(int,input().split()) dx = [1,0,0] dy = [0,1,-1] area = [[0]*w for _ in range(h)] visited = [[False]*w for _ in range(h)] answer = 0 heights = list(map(int,input().split())) def check_available(x,y): # 빗물이 고일 수 ..

[백준 1759번] 암호 만들기

1759번: 암호 만들기 첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다. www.acmicpc.net import copy l, c = map(int,input().split()) alpha_set = set(input().split()) answer = [] vowels = set(['a','e','i','o','u']) # 암호는 서로 다른 L개의 알파벳 소문자들로 구성 # 최소 한 개의 모음(a, e, i, o, u)과 최소 두 개의 자음으로 구성 # 암호를 이루는 알파벳이 암호에서 증가하는 순서로 배열되었을 것 def dfs(string,remain_set):..

[백준 2529번] 부등호

2529번: 부등호 두 종류의 부등호 기호 ‘’가 k개 나열된 순서열 A가 있다. 우리는 이 부등호 기호 앞뒤에 서로 다른 한 자릿수 숫자를 넣어서 모든 부등호 관계를 만족시키려고 한다. 예를 들어, 제시 www.acmicpc.net import copy k = int(input()) signs = list(input().split()) minimum, maximum = float("inf"), float("-inf") def dfs(numbers,num_set): global minimum, maximum, minimum_string, maximum_string length = len(numbers) if length == k+1: # 숫자의 개수가 k+1를 만족할 경우 함수 종료 final_value..

[백준 1240번] 노드사이의 거리

1240번: 노드사이의 거리 첫째 줄에 노드의 개수 $N$과 거리를 알고 싶은 노드 쌍의 개수 $M$이 입력되고 다음 $N-1$개의 줄에 트리 상에 연결된 두 점과 거리를 입력받는다. 그 다음 줄에는 거리를 알고 싶은 $M$개의 노드 쌍 www.acmicpc.net from collections import deque n,m = map(int,input().split()) graph = [[] for _ in range(n+1)] for _ in range(n-1): # n-1개의 간선을 입력받고, 양방향으로 저장 start, end, distance = map(int,input().split()) graph[start].append((end,distance)) graph[end].append((star..

Algorithm/DFS 2024.01.08

[백준 14889번] 스타트와 링크(재풀이)

14889번: 스타트와 링크 예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다. www.acmicpc.net [백준 14889번] 스타트와 링크 14889번: 스타트와 링크 예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다. www.acmicpc.net n = int(input()) visited = [False for _ in range(n)] data = woohyun-king.tistory.com from itertools import combinations from ite..

[백준 11123번] 양 한마리... 양 두마리...

11123번: 양 한마리... 양 두마리... 얼마전에 나는 불면증에 시달렸지... 천장이 뚫어져라 뜬 눈으로 밤을 지새우곤 했었지. 그러던 어느 날 내 친구 광민이에게 나의 불면증에 대해 말했더니 이렇게 말하더군. "양이라도 세봐!" www.acmicpc.net import sys from collections import deque input = sys.stdin.readline t = int(input().rstrip()) dx = [-1,1,0,0] dy = [0,0,-1,1] for _ in range(t): h,w = map(int,input().split()) answer = 0 area = [list(input().rstrip()) for _ in range(h)] visited = [[F..

Algorithm/BFS 2024.01.06

[백준 20291번] 파일 정리

20291번: 파일 정리 친구로부터 노트북을 중고로 산 스브러스는 노트북을 켜자마자 경악할 수밖에 없었다. 바탕화면에 온갖 파일들이 정리도 안 된 채 가득했기 때문이다. 그리고 화면의 구석에서 친구의 메시지를 www.acmicpc.net import sys from collections import Counter input = sys.stdin.readline n = int(input().rstrip()) texts = [input().rstrip().split(".")[1] for _ in range(n)] for name, count in sorted(list(dict(Counter(texts)).items()),key=lambda x: x[0]): print(f"{name} {count}") 이번 ..

[프로그래머스] 숫자의 표현

def solution(n): answer = 0 for i in range(1,n+1): sum_value = 0 for j in range(i,n+1): sum_value += j if sum_value == n: answer += 1 elif sum_value > n: break return answer 이번 문제는 연속된 자연수로 n을 만들 수 있는 모든 경우의 수를 구하는 브루트포스 (brute force, 완전 탐색) 문제이다. 처음에는 dp 문제인 줄 알고 패턴과 점화식을 찾으려고 노력했으나, 도무지 규칙을 생각해봐도 나오지 않아 다른 사람의 풀이를 참고해본 결과 완전 탐색으로 풀이해야 한다는 것을 알게 되었다.(n

[프로그래머스] 최솟값 만들기

import heapq def solution(A,B): result = 0 count = len(A) # A에 대한 최소 힙 / 최대 힙, B에 대한 최소 힙 / 최대 힙 min_heap_a, max_heap_a, min_heap_b, max_heap_b = [], [], [], [] for a in A: heapq.heappush(min_heap_a,a) heapq.heappush(max_heap_a,-a) for b in B: heapq.heappush(min_heap_b,b) heapq.heappush(max_heap_b,-b) while count > 0: # 원소의 개수만큼 반복 min_A, max_A, min_B, max_B = min_heap_a[0], -max_heap_a[0], min_..

Algorithm/Sorting 2024.01.05

[백준 18808번] 스티커 붙이기

18808번: 스티커 붙이기 혜윤이는 최근에 다양한 대회를 참여하면서 노트북에 붙일 수 있는 스티커들을 많이 받았다. 스티커는 아래와 같이 사각 모눈종이 위에 인쇄되어 있으며, 스티커의 각 칸은 상하좌우로 모두 연 www.acmicpc.net n, m, k = map(int,input().split()) paper = [[0]*m for _ in range(n)] count = 0 for _ in range(k): r,c = map(int,input().split()) sticker = [list(map(int,input().split())) for _ in range(r)] def check_available(paper,x,y,sticker,sticker_row_len,sticker_col_len): ..