알고리즘

100일 동안 매일 알고리즘을 하면? - 10일차

유병각 2022. 1. 9. 21:10

100일 동안 매일 알고리즘을 하면? - 10일차

 

https://programmers.co.kr/learn/courses/30/lessons/42628#

 

코딩테스트 연습 - 이중우선순위큐

 

programmers.co.kr

 

문제를 금방 풀 수 있었으나,  힙에 아무것도 없을경우 [0,0] 을 리턴하는 예외처리를 안해줘서 1시간동안 헤매다가 결국 풀었다.

되게 쉬운 문제였고 직관적인 문제였으나 짜잘한 실수들때문에 오래걸렸다.

 

str 형 숫자를 Int 로 바꿔주지 않아서,

예외처리를 안해줘서

import heapq;

def solution(operations):
    minHeap = [];
    maxHeap = [];
    
    for o in operations:
        order, num = o.split(' ');
        if (order == "I"):
            heapq.heappush(minHeap, int(num));
            heapq.heappush(maxHeap, -int(num));
            
        elif (order == 'D' and num == '1'):
            if (len(minHeap) == 0):
                continue;
            dElem = heapq.heappop(maxHeap);
            minHeap.remove(-dElem);
            
        elif (order == 'D' and num == '-1'):
            if (len(minHeap) == 0):
                continue;
            dElem = heapq.heappop(minHeap);
            maxHeap.remove(-dElem);
            
    if (minHeap):
        maxV = -heapq.heappop(maxHeap);
        minV = heapq.heappop(minHeap);
    else:
        maxV = 0;
        minV = 0;
    return [maxV, minV];

https://programmers.co.kr/learn/courses/30/lessons/42587

 

코딩테스트 연습 - 프린터

일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린

programmers.co.kr

deque 에서 queue 처럼 활용할때 pop() 이 아니라, popleft() 를 해야 정상적으로 작동한다....!!!!! 아아아아아아아아

from collections import deque;

def solution(priorities, location):
    queue = deque();
    cnt = 1;
    ii = 0;
    
    for i,p in enumerate(priorities):
        if (i == location) :
            queue.append([p,1]);
        else :
            queue.append([p,0]);
            
    while(queue):
        top = queue[0][0];
        restart = False;
        
        for i in queue:
            print(top, i[0]);
            if top < i[0]:
                restart = True;
                break;
        if (restart):
            temp = queue.popleft();
            queue.append(temp);

        else:
            print('not restart');
            n = queue.popleft();
            if (n[1] == 1):
                return cnt;
            else:
                cnt += 1;