728x90

레벨 2

 

 

heapq (우선 순위 큐) 개념

 

import heapq

list1 = [5,2,6,3,4,1]
heap = []
for i in list1:
	heapq.heappush(heap,i)
print(heap)
[1, 3, 2, 5, 4, 6]

 

heapq는 우선 순위 큐로 heappush() 함수를 사용하면,  k번째 인덱스에 있는 원소가 2k+1, 2k+2 번째 인덱스에 있는 원소보다 같거나 작습니다. 예를 들어, 3이라는 원소는 인덱스 1이고, 인덱스 2k+1(3), 2k+2(4) 에 해당하는 5와 4보다 작은 값을 갖습니다. 

 

import heapq

list1 = [5,2,6,3,4,1]
heap = []
for i in list1:
	heapq.heappush(heap,i)

sort = []
while heap:
    sort.append(heapq.heappop(heap))
print(sort)

 

이 때, heappop() 함수는 heap 리스트를 받아서 원소 값이 가장 작은 값부터 추출을 합니다. 중요한 점은 heap 리스트의 모든 원소가 heappush() 함수로 추가가 되어 있기 때문에 최솟값부터 추출할 수 있다는 것입니다. 만일 heap 리스트가 무작위로 배치되어 있다면 heapify() 함수를 사용합니다. 

 

import heapq

heap = [5,2,6,3,4,1]
heapq.heapify(heap)

sort = []
while heap:
    sort.append(heapq.heappop(heap))
print(sort)

 

 

 

문제풀이

 

import heapq

def solution(scovile, K):
    heap=[]
    # 스코빌 지수 리스트 heapq로 배치
    for i in scovile:
        heapq.heappush(heap,i)
    count=0
      
    while True:
        a = heapq.heappop(heap)
        if a >= K :
            return count
        try :
            b = heapq.heappop(heap)
        except:
            return -1               # heap 이 비어있을 경우
        heapq.heappush(heap,a+2*b)
        count+=1
728x90
728x90

레벨1 K번째 수

def solution(array, commands):
    answer = []
    cut=[]
    for i in range(len(commands)):
        for r in range(commands[i][0]-1,commands[i][1]):
            cut.append(array[r])
        
        cut = sorted(cut)        
        answer.append(cut[commands[i][2]-1])
        cut=[]
    return answer

레벨2 가장 큰 수

def solution(numbers):

    a=[]
    c=''
    for i in numbers:
        a.append([(str(i)*4)[0:4],str(i)])
    a=sorted(a,reverse=True)
    for i in range(len(a)):
        c+=a[i][1]
    if int(c)==0:
        return '0'

    return c

레벨2 H-Index

def solution(citations):
    
    
    while citations:
        a=citations.index(min(citations))
        b=citations.pop(a)
        
        if len(citations)+1 <= b:
            return len(citations)+1
            break
        
    return 0

 

728x90
728x90

레벨2 기능개발

import numpy as np
import math
def solution(progresses, speeds):
    answer = []
    left_time = [100]*len(progresses)
    for i in range(len(left_time)):
        left_time[i] = math.ceil((left_time[i]-progresses[i])/speeds[i])
    print(left_time)
    count = 0
    idx=0
    while idx<len(left_time):
        long = left_time[idx]
        count+=1
        idx+=1
        try:
            while long >= left_time[idx]:
                count+=1
                idx+=1
            answer.append(count)
        except:
            answer.append(count)
        count=0
    return answer

레벨2 프린터

def solution(priorities, location):
    answer = 0
    a=[]
    for i in range(len(priorities)):
        a.append([priorities[i],(len(priorities)*[0])[i]])
    a[location][1] = 1

    while a:
        if max(a)[0] != a[0][0]:
            a.append(a.pop(0))
            print(a)
        elif max(a)[0] == a[0][0]:
            b = a.pop(0)
            answer+=1
            if b[1] ==1:
                break
    return answer

레벨 2 다리를 지나는 트럭

def solution(bridge_length, weight, truck_weights):
    answer = 0
    passing=[]
    finish=[]
    start_time=[0] * len(truck_weights)
    idx=0
    idx2=0
    a=len(truck_weights)
    b=truck_weights.pop(0)
    while a!=len(finish):
        answer+=1 
        if answer - start_time[idx] == bridge_length:
            finish.append(passing.pop(0))
            idx+=1
        if len(truck_weights) != 0:
            if sum(passing)+b <=weight :
                start_time[idx2]=answer
                passing.append(b)
                idx2+=1           
                b=truck_weights.pop(0)
        elif len(truck_weights) == 0:
            if 0<sum(passing)+b <=weight :
                start_time[idx2]=answer
                passing.append(b)
                answer = start_time[idx2] + bridge_length
                break

    return answer

 

레벨 2 주식 가격

def solution(prices):
    answer = [0]*len(prices)
    for i in range(len(prices)-1):
        for j in range(i+1,len(prices)):
            if prices[i] <= prices[j]:
                answer[i]+=1
            else:
                answer[i]+=1
                break
    return answer
728x90

+ Recent posts