sugenius

[프로그래머스/코딩테스트 고득점 Kit/Python] 스택/큐 본문

PROGRAMMERS/Coding Test, Python

[프로그래머스/코딩테스트 고득점 Kit/Python] 스택/큐

sugeniusk 2023. 12. 6. 21:35

https://school.programmers.co.kr/learn/challenges?tab=algorithm_practice_kit 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


<스택/큐>
Q.같은 숫자는 싫어 

def solution(arr):
    answer = []
    for a in arr : 
        if a not in answer or a!= answer[-1] : 
            answer.append(a)
    return answer



Q.기능개발 #***

'''
#틀림 --> x > dq[0] 에서 부등호 빠짐 ㅠ x>=q[0] 이여야함 
#반례 : [90, 90, 90, 90],[30, 1, 1, 1],[1, 3] 
from collections import deque 
def solution(progresses, speeds):
    answer = []
    dq=[]
    for p,s in zip(progresses,speeds) : 
        n=(100-p)//s 
        if n*s+p < 100 : 
            n+=1 
        dq.append(n)
        
    dq=deque(dq)
    cnt=0
    
    while True : 
        if len(dq)==0 : 
            break 
            
        x=dq.popleft()
        cnt=1
        while dq and x > dq[0] : 
            dq.popleft()
            cnt+=1
        answer.append(cnt)
    
    return answer

'''
from collections import deque 
def solution(progresses, speeds):
    answer = []
    dq=[]
    dq=deque(dq)
    
    for p,s in zip(progresses,speeds) : 
        n=(100-p)//s 
        while True : 
            if n*s+p < 100 : 
                n+=1
            else : 
                break
        
        dq.append(n)
    
    while True : 
        if len(dq)==0 : 
            break 
        if len(dq)==1 : 
            answer.append(1)
            break 
            
        x=dq.popleft()
        cnt=1
        while dq and x >= dq[0] : 
            dq.popleft()
            cnt+=1
            
        answer.append(cnt)
    
    return answer


Q.올바른 괄호 

def solution(s):
    answer = True
    stack=[] 
    
    if s[0] == ')' : 
        return False 
    
    for x in s : 
        if x=='(' : 
            stack.append(x) 
        else : #')'
            if len(stack)==0 : 
                return False 
            stack.pop()
            
    if len(stack) > 0 : 
        return False

    return True


Q.프로세스 #***

from collections import deque 
def solution(priorities, location):
    answer = 0
    dq = deque() 
    for i,p in enumerate(priorities) :  
        dq.append((i,p))
        
    while True : 
        x=dq.popleft()
        if any(x[1]<d[1] for d in dq) : 
            dq.append(x)
        else : 
            answer+=1
            if x[0] == location :
                break
    return answer
#queue =  [(i,p) for i,p in enumerate(priorities)]


Q.다리를 지나는 트럭 #***

'''
#틀림
from collections import deque 
def solution(bridge_length, weight, truck_weights):
    answer = 0
    dq=deque([0]*bridge_length)
    dq=deque(dq)
    truck_weights = deque(truck_weights)
    while True : 
        if len(truck_weights) == 0 and len(dq) == 0 : 
            break 
            
        if len(truck_weights) > 0 and (sum(dq)+truck_weights[0]) <= weight : 
            dq.append(truck_weights.popleft())
        else : 
            dq.popleft()
        answer+=1
        
    return answer

'''
from collections import deque 
def solution(bridge_length, weight, truck_weights):
    answer = 0
    
    #시간 초과 / 5번 테스트 케이스만 .. >> sum() 함수 : O(n) 이라 김  um보다는 변수를 가감하는 방식으로 변경해야 
    dq=deque([0]*bridge_length)
    truck_weights = deque(truck_weights)
    sum_truck= 0 
    
    while dq:
        answer+=1
        sum_truck-=dq.popleft()
        if truck_weights:
            #if sum(dq)+truck_weights[0]<=weight:
            if sum_truck+truck_weights[0]<=weight:
                x=truck_weights.popleft()
                sum_truck+=x
                dq.append(x)
            else:
                dq.append(0)
    
    return answer


Q.주식가격 #***

def solution(prices):
    answer = []
    
    '''
    #테스트 케이스 1번 제외 하고 틀림 
    #반례 [4,5,1,2,6,1,1], [2,1,4,2,1,1,0] 
    for i, p in enumerate (prices) : 
        cnt=0
        print(p, prices[i+1:])
        for j in prices[i+1:] : 
            if p <= j : 
                cnt+=1
        answer.append(cnt)
    
    '''

    '''
    #효율성 테스트 모두 실패 
    for i,p in enumerate (prices) : 
        cnt=0
        stack=[]
        for t in prices[i+1:] : 
            stack.append(t)
            if stack[-1] < p :
                cnt=len(stack)
                break
            else : 
                cnt+=1
        answer.append(cnt)
    '''

    return answer

from collections import deque 
def solution(prices):
    answer = []
    dq=deque(prices)
    
    while dq : 
        x=dq.popleft()
        cnt=0
        for i in dq : 
            cnt+=1 
            if i < x :
                break 
        answer.append(cnt)
    return answer