[Programmers] 프린터 / 다리를 지나는 트럭 / 기능개발 / 124 나라의 숫자 (python)

yourmean·2021년 2월 14일
0

Algorithm - Programmers

목록 보기
13/13
post-thumbnail

작성 중인 글이 10개가 넘었다,, 어서 정리해서 마무리해야지
오늘은 오랜만에 알고리즘으로 데려왔다 :-)

🌴 프린터

문제 링크

해결 전략

priorities : 현재 대기목록에 있는 문서의 중요도가 순서대로 담긴 배열
location : 내가 인쇄를 요청한 문서의 현재 대기목록에서의 위치

  1. 현재 맨 앞의 문서를 꺼내서(pop(0)) priorities의 최댓값과 동일하면 인쇄(answer+=1)
  2. 1의 문서가 요청문서라면 break, 아닌 경우 location을 한 칸 땡겨주고 반복
  3. 1~2의 경우가 아니라면 문서 마지막으로 이동

Source Code

def solution(priorities, location):
    answer = 0
    m= max(priorities)
    
    while True:
        m= max(priorities)
        c= priorities.pop(0)
        
        # 현재문서 인쇄
        if c==m:
            answer+=1
            if location==0:
                break
            else:
                location-= 1
                
        # 현재문서 -> 대기목록 마지막으로
        else:
            priorities.append(c)
            location= location-1 if location !=0 else len(priorities)-1
            
    return answer

🌴 다리를 지나는 트럭

문제 링크

해결 전략

bridge_length : 다리 길이
weight 다리가 견딜 수 있는 무게
truck_weights : 트럭별 무게

Source Code

def solution(bridge_length, weight, truck_weights):
    answer = 0
    bridge= [0] *bridge_length
    tot_w= 0
    
    while truck_weights:
        tot_w-= bridge.pop(0)
        if tot_w + truck_weights[0] <= weight:
            a= truck_weights.pop(0)
            bridge.append(a)
            tot_w+= a
        else:
            bridge.append(0)
        answer+= 1
        
    return answer+bridge_length

해결하는 데 매우 오래 걸렸던 문제 (3일 걸렸음ㅎ)



🌴 기능개발

문제 링크

해결 전략

progresses : 배포되어야 하는 순서대로 작업의 진도가 적힌 정수 배열
speeds : 각 작업의 개발 속도가 적힌 정수 배열

Source Code

def solution(progresses, speeds):
    answer = []
    day= [] # 기능별 완료까지 걸리는 일수
    
    for i in range(len(progresses)):
        temp= (99-progresses[i]) // speeds[i] + 1
        day.append(temp)

    while len(day) > 0:
        cnt = 1
        a = day.pop(0)
        day2 = day.copy()
        for i in range(len(day)):
            if a >= day[i]:
                cnt += 1
                day2.pop(0)
            else:
                break
        answer.append(cnt)
        day = day2.copy()

    return answer


🌴 124 나라의 숫자

문제 링크

해결 전략

progresses : 배포되어야 하는 순서대로 작업의 진도가 적힌 정수 배열
speeds : 각 작업의 개발 속도가 적힌 정수 배열

  1. 1,2,4가 반복되고 있고, 십진법에서의 수는 다음과 같이 나타난다.
    한자릿수 3개: 1 2 4
    두자릿수 9개: 11 12 14 21 22 24 41 42 44
    세자릿수 27개: 111
    따라서 자릿수가 증가할 때마다 3의 거듭제곱만큼 개수 포함

Source Code

def solution(n):
    answer = ''
    num= [1, 2, 4]
    
    while n>0:
        temp= num[(n-1)%3]
        answer+= str(temp)
        n= (n-1)//3

    return answer[::-1]
profile
𝐼 𝑒𝑖𝑡ℎ𝑒𝑟 𝑤𝑖𝑛 𝑜𝑟 𝑙𝑒𝑎𝑟𝑛 💪🏻

0개의 댓글