プログラマ層2-機能開発



に答える



問題を解くのは簡単だ
プロセスと速度は同時にO(N)を必要とする

リストqueueの利用

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

実際,list queueを用いる方法はO(N)でなければならない.
collections.Dequeの使用がより良い

collections.Dequeの利用

from collections import deque 

def solution(progresses, speeds):
    answer = []
    days = 1
    count = 0
    pr = deque(progresses)
    sp = deque(speeds)
    
    while pr :
        if (pr[0] + sp[0] * days) >= 100 : 
            count += 1
            pr.popleft()
            sp.popleft()
        else :
            if count > 0 :
                answer.append(count)
                count = 0
            days += 1
    
    answer.append(count)
        
    return answer