프로그래머스-기능개발(파이썬, python)

SA Jung·2022년 9월 30일
0

Programmers 문제 풀이

목록 보기
3/14

https://school.programmers.co.kr/learn/courses/30/lessons/42586

[git]
https://github.com/JungSangA/Algorithm_Study/blob/main/%EC%8A%A4%ED%83%9D%26%ED%81%90/%EA%B8%B0%EB%8A%A5%20%EA%B0%9C%EB%B0%9C.ipynb

1. idx 순차증가 비교를 통한 풀이

import math
def solution(progresses, speeds):
    answer = []
    time_list = []
    cnt = 0
    # 남은 일수 계산
    for i,j in zip(progresses, speeds):
        time = math.ceil((100-i)/j)
        time_list.append(time)
    idx = 0
    for i in range(len(time_list)):
        if time_list[idx] < time_list[i]:
            answer.append(i-idx)
            idx = i
        # 마지막 idx일시 진행
        if i == len(time_list)-1:
            answer.append(len(time_list)-idx)
    return answer
  • progress의 남은 퍼센트와 speeds를 나누어 남은 일수를 계산한다.(math.ceil을 통해 소수점이 있으면 올림을 통해 남은 일수를 계산하는 것이 핵심이다.)

    [math.ceil 함수 정리]
    https://velog.io/@jsanga214/%EC%BD%94%ED%85%8C%EC%A4%80%EB%B9%84-math

  • idx를 0으로 고정하고 for문을 통해 0부터 순차적으로 증가하면서 idx와 크기 비교를 진행한다.

  • idx보다 for문을 통한 i의 인덱스 값이 더 크다면 i-idx를 통해 그만큼의 갯수를 계산하여 answer에 추가하고, 변수 idx에 i를 넣고 계속 진행한다.

  • for문을 순차적으로 증가시키고 마지막 idx의 값일 경우 무조건 배포가 되기 때문에 answer에 추가하고 최종적으로 answer을 return 한다.

2. while문과 Queue를 통한 풀이

import math
from collections import deque

def solution(progresses, speeds):
    answer = []
    time_list = deque()
    cnt = 1
    ## 남은 일수 비교
    for i, j in zip(progresses, speeds):
        time = math.ceil((100-i)/j)
        time_list.append(time)

    # 초기값 설정
    cur_progress = time_list.popleft()
    while len(time_list) > 0:
        next_progress=time_list.popleft()
        # 현재 대기열의 남은 일수와 다음 대기열의 남은 일수를 비교
        if cur_progress >= next_progress:
            cnt +=1
        else:
            answer.append(cnt)
            cur_progress = next_progress
            cnt = 1
        # time_list의 마지막일 때 answer에 값 추가
        if len(time_list)==0:
            answer.append(cnt)
    return answer
  • 남은일수 계산은 위와 동일하다.
  • 초기값으로 popleft()를 통해 현재 진행 프로세스에 저장한다.
  • while문 안에서 현재 진행 프로세스의 남은 일수와 다음 진행 프로세스의 남은 일수를 비교하여 같이 배포할지 함께 배포할지를 결정하는 알고리즘을 time_list의 모든 값들이 반환될 때까지 반복한다.
  • 현재 진행 프로세스보다 다음 진행 프로세스의 남은 일수가 더 적거나 같다면 다음 진행 프로세스는 현재 진행 프로세스가 끝날때까지 배포를 못하므로 cnt를 증가시키고, 다음 진행 프로세스를 popleft()를 통해 반환하여 계속 비교한다.
  • 현재 진행 프로세스와 다음 진행 프로세스가 크다면 그 부분까지 배포를 한번에 하므로 cnt를 answer에 추가하고, cnt를 1로 초기화하고, 현재 진행 프로세스에 새롭게 다음 진행 프로세스를 덮어 씌워서 while문을 돌도록 한다.
  • 위와 동일하게 time_list의 마지막 idx일때 무조건 배포를 하므로 answer에 값을 추가하고 종료한다.
profile
Tomorrow will be better than yesterday :)

0개의 댓글