프로그래머스 고득점 Kit(Python) - 2. 스택/큐

안예진·2021년 5월 5일

programmersKit

목록 보기
2/5
post-thumbnail

스택/큐 -> python에서는 list.pop()/pop(0) 사용

기능개발

progresses/speeds에서 하나씩 뽑아온다
현재까지 지난 days동안 100퍼센트 넘게 채운다면 cnt+1
현재만으로는 부족하다면 지금까지 cnt answer에 채우기 & days 세팅,cnt = 1
while문 끝나면 남아있는 cnt 더하고 맨앞에 더해진 0 없애주기

import math
def solution(progresses, speeds):
    answer = []
    days = 0
    cnt = 0
    while progresses:
        current = progresses.pop(0)
        speed = speeds.pop(0)
        
        if current + days*speed >= 100:
            cnt += 1
        else:
            answer.append(cnt)
            days = math.ceil((100-current)/speed)
            cnt = 1
            
    answer.append(cnt)  
    answer.pop(0)
    return answer
def solution(progresses, speeds):
    Q=[]
    for p, s in zip(progresses, speeds):
        if len(Q)==0 or Q[-1][0]<-((p-100)//s):
            Q.append([-((p-100)//s),1])
        else:
            Q[-1][1]+=1
    return [q[1] for q in Q]

프린터

queue에 [우선순위, location] 넣기
priorities는 우선순위 높은순으로 정렬
가장 높은 우선순위인 current를 만나면 pop & order+1 /아니면 append

def solution(priorities, location):
    queue = []
    order = 0
    for idx,priority in enumerate(priorities):
        queue.append([priority, idx])
    
    priorities.sort(reverse=True)
    
    while True:
        current = queue.pop(0)
        if current[0] == priorities[0]:
            priorities.pop(0)
            order += 1
            if current[1] == location:
                break
        else:
            queue.append(current)
    return order     
def solution(priorities, location):
    queue =  [(i,p) for i,p in enumerate(priorities)]
    answer = 0
    while True:
        cur = queue.pop(0)
        if any(cur[1] < q[1] for q in queue):
            queue.append(cur)
        else:
            answer += 1
            if cur[0] == location:
                return answer

다리를 지나는 트럭

  1. 맨처음 트럭부터 넣고 시작
  2. checkOnBridge 통해서 한칸씩 이동 & 남은길이0인 트럭제외 무게합,현재리스트 리턴
  3. 대기 1번 트럭을 넣을 수 있다면 넣기
def solution(bridge_length, weight, truck_weights):
    answer = 1
    onBridge = [[truck_weights.pop(0),bridge_length-1]]
    
    while onBridge:
        answer += 1
        sumWeight,onBridge = checkOnBridge(onBridge)
        if len(truck_weights) == 0: continue
        if sumWeight + truck_weights[0] <= weight:
            sumWeight += truck_weights[0]
            onBridge.append([truck_weights.pop(0),bridge_length-1])
        # print(answer,sumWeight,onBridge)  
    return answer


def checkOnBridge(onBridge):
    idx = 0
    sumWeight = 0;
    while idx < len(onBridge):
        if onBridge[idx][1] == 0:
            onBridge.pop(idx)
        else:
            onBridge[idx][1] -= 1
            sumWeight += onBridge[idx][0]
            idx += 1
    
    return sumWeight,onBridge

주식가격

현재값을 기준으로 이후의 값을 비교하면서 버틴초를 계산한다

def solution(prices):
    answer = []
    
    for cur in range(len(prices)):
        temp = 0
        for comp in range(cur+1,len(prices)):
            temp += 1
            if prices[cur] > prices[comp]:
                break
        answer.append(temp)
            
    return answer
def solution(prices):
    answer = [0] * len(prices)
    for i in range(len(prices)):
        for j in range(i+1, len(prices)):
            if prices[i] <= prices[j]:
                answer[i] += 1
            else:
                answer[i] += 1
                break
    return answer
profile
에국은 에구구구...

0개의 댓글