본문 바로가기

알고리즘

(138)
힙 - 이중우선순위큐 / 파이썬 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]
스택 - 기능개발 / 파이썬 def solution(progresses, speeds): answer, lst = [], [] for p, s in zip(progresses, speeds): cnt = 0 while p < 100: p += s cnt += 1 lst.append(cnt) front = 0 for i in range(len(lst)): if lst[front] < lst[i]: answer.append(i - front) front = i answer.append(len(lst)-front) return answer programmers.co.kr/learn/courses/30/lessons/42586# 코딩테스트 연습 - 기능개발 프로그래머스 팀에서는 기능 개선 작업을 수행 중입니다. 각 기능은 진도가 100%일..
스택 - 주식가격 / 파이썬 def solution(prices): answer = [0] * len(prices) for i in range(len(prices)-1): for j in range(i+1, len(prices)): if prices[j] < prices[i]: answer[i] += 1 break else: answer[i] += 1 return answer programmers.co.kr/learn/courses/30/lessons/42584 코딩테스트 연습 - 주식가격 초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00 programmer..
bfs - 단어 변환 / 파이썬 from collections import deque def ck(w1, w2): cnt = 0 lenth = len(w1) for i in range(lenth): if w1[i] == w2[i]: cnt += 1 return True if cnt == lenth-1 else False def solution(begin, target, words): if target not in words: return 0 q = deque() for w in words: if ck(begin, w): q.append([w, 1]) while q: word, cnt = q.popleft() if word == target: return cnt for w in words: if ck(word, w): q.append([..