알고리즘/프로그래머스(Python) (39) 썸네일형 리스트형 그리디 - 구명보트 / 파이썬 def solution(people, limit): answer, front, back = 0, 0, len(people)-1 people.sort(reverse=True) while front 그리디 - 조이스틱 / 파이썬 def solution(name): lst = [min(ord(n)-ord('A'), ord('Z')-ord(n)+1) for n in name] idx, answer = 0, 0 while True: answer += lst[idx] lst[idx] = 0 if sum(lst) == 0: break left, right = 1, 1 while lst[idx-left] == 0: left += 1 while lst[idx+right] == 0: right += 1 if left < right: answer += left idx -= left else: answer += right idx += right return answer programmers.co.kr/learn/courses/30/lessons/.. 그래프 - 가장 먼 노드 / 파이썬 from collections import deque def bfs(n, adj): q = deque() q.append([1, 0]) ck = [0] * n while q: cnt, nod = q.popleft() if ck[nod] == 0: ck[nod] = cnt cnt += 1 for a in adj[nod]: q.append([cnt, a]) ret = 0 for c in ck: if c == max(ck): ret += 1 return ret def solution(n, edge): adj = [[] for _ in range(n)] for e in edge: x = e[0]-1 y = e[1]-1 adj[x].append(y) adj[y].append(x) return bfs(n, adj).. 힙 - 이중우선순위큐 / 파이썬 import heapq def reset(heap): temp = [] for h in heap: heapq.heappush(temp, -h) return temp def solution(operations): min_heap, max_heap = [], [] for oper in operations: op, num = oper.split() num = int(num) if op == 'I': heapq.heappush(min_heap, num) heapq.heappush(max_heap, -num) elif min_heap and op == 'D': if num == 1: h = heapq.heappop(max_heap) min_heap = reset(max_heap) else: heapq.heappo.. 힙 - 디스크 컨트롤러 / 파이썬 import heapq def solution(jobs): answer, cnt, last = 0, 0, -1 heap = [] jobs.sort() time = jobs[0][0] while cnt < len(jobs): for s, t in jobs: if last < s 힙 - 더 맵게 / 파이썬 import heapq def solution(scoville, K): answer = 0 heapq.heapify(scoville) while True: if len(scoville) == 1 and scoville[0] = K: break return answer programmers.co.kr/learn/courses/30/lessons/42626 코딩테스트 연습 - 더 맵게 매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 .. 큐 - 프린터 / 파이썬 from collections import deque def solution(priorities, location): lst = [i for i in range(len(priorities))] pri = deque(priorities) loc = deque(lst) answer = 0 while True: front = pri.popleft() l = loc.popleft() if pri and front < max(pri): pri.append(front) loc.append(l) else: answer += 1 if l == location: break return answer 다른 풀이 def solution(priorities, location): answer = 0 from collections.. 큐 - 다리를 지나는 트럭 / 파이썬 from collections import deque def solution(bridge_length, weight, truck_weights): answer = 0 q = [0] * bridge_length q = deque(q) truck_weights = deque(truck_weights) while q: answer += 1 q.popleft() if truck_weights: if sum(q) + truck_weights[0] 이전 1 2 3 4 5 다음