알고리즘 공부 13일차

김서영·2024년 1월 22일
0

알고리즘

목록 보기
13/25

1. 백준 - 벌집

N = int(input())
cnt = 1
num = 6
room = 1

if N==1:
    print(1)
else:
    while True:
        cnt += num
        room += 1
        if N <= cnt:
            print(room)
            break
        num += 6

2. 백준 - 영단어 암기는 어려워

import sys
sys.stdin = open('input.txt', 'r')
input = sys.stdin.readline
from collections import Counter

N, M = map(int, input().rstrip().split())
words = [input().rstrip() for _ in range(N)]
words_dict = Counter(words).most_common()
words_dict.sort(key=lambda x: (-x[1], -len(x[0]), x[0]))# 자주 나오는 단어일수록, 단어 길이가 길수록, 알파벳 사전 순
for i in words_dict:
    if len(i[0]) >= M:
        print(i[0])

3. 백준 - 지름길

import sys
sys.stdin = open('input.txt', 'r')
input = sys.stdin.readline

N, D = map(int, input().rstrip().split()) # 지름길 개수, 고속도로 길이

G = [list(map(int, input().rstrip().split())) for _ in range(N)]

lst = [i for i in range(D+1)]

for i in range(D+1):
    lst[i] = min(lst[i-1]+1, lst[i]) # 이전 위치에서 1 더한 값과 현재 위치 값 중 작은 값 (이전 위치에서 지름길로 이동했으면 1더해도 훨씬 작겠징??)
    for start, end, gireumgil in G:
        if i == start and end <= D and lst[i] + gireumgil < lst[end]:
            # 지름길 시작 위치가 start와 같고
            # 도착 위치가 최종 목적지까지의 거리보다 작거나 같고
            # 현재위치 + 지름길 거리가 도착 위치보다 작고
            lst[end] = lst[start] + gireumgil

print(lst[D])

4. 백준 - 올림픽

N, K = map(int, input().split())
medal = [list(map(int, input().split())) + [0] for _ in range(N)]
medal.sort(key=lambda x:(x[1], x[2], x[3]), reverse=True)

rank = 1
for i in range(N):
    if medal[i][4] == 0:
        medal[i][4] = rank
    for j in range(i+1, N):
        if medal[i][1:4] == medal[j][1:4]:
            if medal[j][4] == 0:
                medal[j][4] = rank
    rank += 1

for i in range(N):
    if medal[i][0] == K:
        print(medal[i][4])

5. 백준 - 최소 힙

import sys
input = sys.stdin.readline
from heapq import heappop, heappush

N = int(input())
lst = []
for i in range(N):
    x = int(input())
    if x:
        heappush(lst, x)
        continue
    if not lst:
        print(0)
    else:
        print(heappop(lst))
profile
개발과 지식의 성장을 즐기는 개발자

0개의 댓글