본문 바로가기

알고리즘 배우기20

[백준] Bronze 1. 1236번: 성 지키기 # n과 m을 입력받기n, m = map(int, input().split())no_n = set()no_m = set()# n개의 행을 입력받아 처리for i in range(n): ch = input() l = list(ch) for j in range(m): if l[j] == 'X': no_n.add(i) no_m.add(j)# no_n과 no_m의 길이len_n = n - len(no_n)len_m = m - len(no_m)# 큰 거를 고르면 되는 이유는 최소의 경비원만 설치한다는 의미# 최적의 선택을 한다는 가정 하에, (가로 세로)를 한 번에 지우는 경우를 먼저 선택하므로# 작은 쪽은 자동으로 만족된다.big = max(l.. 2024. 7. 16.
[프로그래머스] Lv 2. 전화번호 목록 def solution(phone_book): hash = {} for n in phone_book: hash[n] = 1 for num in phone_book: arr = "" for n in num: arr += n if arr in hash and arr != num: return False return Truehttps://mainkey.tistory.com/20 [프로그래머스] 전화번호 목록 _ Python 해시Lv.2"전화번호 목록" (출처:프로그래머스) 문제 설명 전화번호부에 적힌 전화번호 중, 한 번호가 다른 번호의 접두어인 경우가 있는지 확인하려 합니다. .. 2024. 7. 16.
[알고리즘 정리] 다익스트라 import heapqdef dijkstra(adj_matrix, start): num_nodes = len(adj_matrix) # 각 노드까지의 최단 거리 초기화 distances = [float('infinity')] * num_nodes distances[start] = 0 # 우선순위 큐 초기화 priority_queue = [(0, start)] # (거리, 노드) while priority_queue: current_distance, current_node = heapq.heappop(priority_queue) # 현재 노드의 거리가 저장된 거리보다 크면 무시 if current_distance > distance.. 2024. 7. 14.
[프로그래머스] Lv 2. H-index def solution(citations): num_paper = len(citations) #citation.sort(reverse=True) def quick_sort_reverse(citations): if len(citations) pivot] lesser = [x for x in citations[1:] if x 2024. 7. 14.