2021-08-06 문제풀기

골솔·2021년 8월 6일
0

알고문제풀기

목록 보기
20/27

프로그래머스

다리를 지나는 트럭

from collections import deque
def solution(bridge_length, weight, truck_weights):
    answer = 0
    que = deque()
    n = len(truck_weights)
    truck_weights = deque(truck_weights)
    t = 0
    trucks = 0
    while truck_weights:
        if len(que)>0:
            w, time = que[0]
            if time == t:
                que.popleft()
                trucks -= w
        if bridge_length > len(que) and weight >= trucks+truck_weights[0]:
            temp = truck_weights.popleft()
            que.append((temp, t+bridge_length))
            trucks += temp
        t += 1
    answer = t+bridge_length
    return answer

H-index

def solution(citations):
    citations.sort(reverse=True)
    n = len(citations)
    answer = 0
    for i in range(n):
        if citations[i] <= i:
            answer = i
            break
    return answer

입국심사

def solution(n, times):
    answer = 0
    left = 1
    right = n * max(times)
    while left < right:
        mid = (left + right) // 2
        temp = 0
        for time in times:
            temp += mid // time
        if temp >= n:
            right = mid
        else:
            left = mid+1
        answer = left
    return answer

가장 큰 수

def solution(numbers):
    numbers = list(map(str, numbers))
    numbers.sort(reverse=True, key=lambda x:x*5)
    answer = str(int(''.join(numbers)))
    return answer

이중우선순위큐

import heapq
def solution(operations):
    answer = [0,0]
    min_heap = []
    max_heap = []
    for op in operations:
        num = int(op[2:])
        if op[0]=="I":
            heapq.heappush(min_heap, num)
            heapq.heappush(max_heap, -num)
        else:
            if len(min_heap) > 0:
                if num == 1: # 최댓값 삭제
                    max_num = heapq.heappop(max_heap)
                    min_heap.remove(-max_num)
                else: # 최솟값 삭제
                    min_num = heapq.heappop(min_heap)
                    max_heap.remove(-min_num)
    if len(min_heap) != 0:
        answer = [-max_heap[0], min_heap[0]]
    return answer
profile
골때리는이솔

0개의 댓글