문제
프로그래머스 팀에서는 기능 개선 작업을 수행 중입니다. 각 기능은 진도가 100%일 때 서비스에 반영할 수 있습니다.
또, 각 기능의 개발속도는 모두 다르기 때문에 뒤에 있는 기능이 앞에 있는 기능보다 먼저 개발될 수 있고, 이때 뒤에 있는 기능은 앞에 있는 기능이 배포될 때 함께 배포됩니다.
먼저 배포되어야 하는 순서대로 작업의 진도가 적힌 정수 배열 progresses와 각 작업의 개발 속도가 적힌 정수 배열 speeds가 주어질 때 각 배포마다 몇 개의 기능이 배포되는지를 return 하도록 solution 함수를 완성하세요.
입출력 예
progresses | speeds | return |
[93, 30, 55] | [1, 30, 5] | [2, 1] |
[95, 90, 99, 99, 80, 99] | [1, 1, 1, 1, 1, 1] | [1, 3, 2] |
풀이
내 아이디어: 새로운 list(queue)에 각 progress마다 필요한 날짜(기간)을 담고 앞에서부터 비교해서 뒤의 값들보다 클 때 pop하고 개수 세자
막힌 부분: 앞에서 부터 뒤의 값과 비교
해결책: 현재 index보다 더 큰 값을 가진 index를 뒤에서 찾고 그 앞의 원소들 개수를 (index끼리 빼준 값) answer에 append
import math
def solution(progresses, speeds):
answer = []
day = []
for i in range(len(progresses)):
temp = math.ceil((100-progresses[i])/speeds[i])
day.append(temp)
index = 0
for i in range(len(day)):
if day[index] < day[i]:
answer.append(i-index)
index = i
answer.append(len(day)-index)
return answer
주의할 점: 날짜 계산할 때 올림(ceil) 이용
간단하게 표현하기
day = [math.ceil((100-p/s) for p,s in zip(progresses, speeds)]
다른 풀이
from collections import deque
def solution(progresses, speeds):
answer= []
day = 0 #배포 날짜
count = 0 #배포된 작업 수
while len(progresses) > 0: #작업 완료
if(progresses[0] + day* speeds[0]) >= 100:
progresses.pop(0)
speeds.pop(0)
count +=1
else:
day +=1
if count > 0: #현재 작업 진도는 100%가 아닌데 앞에서 배포함
answer.append(count)
count= 0
answer.append(count)
return answer
'코테 공부 > 프로그래머스' 카테고리의 다른 글
[Python/DP] 11727 2xn 타일링2(점화식 찾기 어려움) (0) | 2022.08.09 |
---|---|
[Python/그리디] 체육복 (0) | 2022.08.03 |
[Python/BFS] 타겟넘버 (0) | 2022.08.03 |
[Python/Heap] 이중우선순위큐 (0) | 2022.08.02 |
[Python/완전탐색] 카펫 (0) | 2022.08.02 |