반응형

 출처 : 프로그래머스

 

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

 

programmers.co.kr

def solution(operations):
    answer = []
    operations_after = []
    for operation in operations:
        opr, number = operation.split(" ")
        if opr == "I":
            operations_after.append(int(number))
        elif opr == "D" and number == "-1" and len(operations_after) > 0:
            operations_after.remove(min(operations_after))
        elif opr == "D" and number == "1" and len(operations_after) > 0:
            operations_after.remove(max(operations_after))
    opr_max = 0
    opr_min = 0
    if len(operations_after) >= 2:
        opr_max, opr_min = max(operations_after), min(operations_after)
    elif len(operations_after) == 1:
        if max(operations_after) < opr_max:
            opr_min = max(operations_after)
        else:
            opr_max = max(operations_after)
    answer.append(opr_max)
    answer.append(opr_min)
    return answer

#operations = ["I 16","D 1"]
operations = ["I 16", "I -5643", "D -1", "D 1", "D 1", "I 123", "D -1"]
print(solution(operations))
#operations= ["I 7","I 5","I -5","D -1"]
operations = 	["I -45", "I 653", "D 1", "I -642", "I 45", "I 97", "D 1", "D -1", "I 333"]
print(solution(operations))
반응형

'문제 > 프로그래머스' 카테고리의 다른 글

피로도  (0) 2022.08.06
H-Index  (0) 2022.06.30
디스크 컨트롤러  (0) 2022.06.15
주식가격  (0) 2022.06.07
다리를 지나는 트럭  (0) 2022.06.02

+ Recent posts