[스택/큐] 모음_코딩테스트 고득점 Kit

EunBi Na·2023년 2월 11일
0

같은 숫자는 싫어

링크텍스트

def solution(arr):
    b = []
    for i in range(len(arr)):
        if i == 0:
            b.append(arr[i])
        elif arr[i] != arr[i-1]:
            b.append(arr[i])

    return b
def no_continuous(s):
    a = []
    for i in s:
        if a[-1:] == [i]: continue
        a.append(i)
    return a

기능개발

링크텍스트

def solution(progresses, speeds):
    print(progresses)
    print(speeds)
    answer = []
    time = 0
    count = 0
    while len(progresses)> 0:
        if (progresses[0] + time*speeds[0]) >= 100:
            progresses.pop(0)
            speeds.pop(0)
            count += 1
        else:
            if count > 0:
                answer.append(count)
                count = 0
            time += 1
    answer.append(count)
    return answer

올바른 괄호

링크텍스트

def solution(s):
    stack = []
    for i in s:
        if i == "(":
            stack.append(i)
        else:
            if stack:
                stack.pop()
            else:
                return False
    if stack:
        return False
    return True

프린터

링크텍스트

def solution(priorities, location):
    answer = 0
 
    while len(priorities) > 0:
        if priorities[0] == max(priorities):  # 우선순위 확인
            # 맨 앞 문서 인쇄
            priorities.pop(0)
            answer += 1
            if location == 0:  # 요청한 문서이면 반복문 종료
                break
        else:
            # 맨 앞 문서를 대기 목록의 가장 마지막에 추가
            priorities.append(priorities.pop(0))
        location = location - 1 if location > 0 else len(priorities) - 1  # 요청 문서 위치 변경
    return answer

다리를 지나는 트럭

링크텍스트

def solution(bridge_length, weight, truck_weights):
    q = [0] * bridge_length
    time = 0
    
    while q:
        time += 1
        q.pop(0)
        if truck_weights:
            if sum(q) + truck_weights[0] <= weight:
                q.append(truck_weights.pop(0))
            else:
                q.append(0)
    
    return time

주식가격

링크텍스트

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
This is a velog that freely records the process I learn.

0개의 댓글