파이썬으로 부시자! Python with Coreenee

프로그래머스 - Heap - 디스크 컨트롤러

문제

스크린샷 2020-04-17 오후 3 09 16 스크린샷 2020-04-17 오후 3 09 26 스크린샷 2020-04-17 오후 3 09 34 스크린샷 2020-04-17 오후 3 09 44

입력

스크린샷 2020-04-17 오후 3 09 50


나의 처음 코드

운영체제 과목을 이수한 사람이라면, 문제를 봤을 때 매우 익숙할 것이다! 맞다! 바로 “스케줄링 알고리즘”으로 배웠던 문제이다. 직전 학기에 운영체제 과목을 들었기 때문에 긴 글의 문제를 이해하는데 많은 시간이 걸리지 않았다. 컴퓨터공학도라면 스케줄링 알고리즘에 대해서 검색을 하여 스스로 터득하도록 하자! 문제만 이해하면 바로 풀수 있는 문제!

def solution(jobs):
    answer = 0
    end = 0
    j_size = len(jobs)
    tmp_stime = []
    tmp_dtime = []
    jobs.sort(key = lambda x : (-x[0], -x[1]))
    now = jobs.pop()
    end = now[0] + now[1]
    answer += now[1]
    
    while True:
        while len(jobs) != 0:
            if jobs[-1][0] <= end:
                tmp = jobs.pop()
                tmp_stime.append(tmp[0])
                tmp_dtime.append(tmp[1])
            else :
                break
        if len(tmp_stime) != 0 and len(tmp_dtime) != 0:
            tmp_min = min(tmp_dtime)
            end = end + tmp_min 
            answer += (end - tmp_stime[tmp_dtime.index(tmp_min)])
            del tmp_stime[tmp_dtime.index(tmp_min)] 
            del tmp_dtime[tmp_dtime.index(tmp_min)]
        else:
            if len(jobs) != 0:
                tmp = jobs.pop()
                diff = tmp[0] - end
                end = end + diff + tmp[1]
                answer += tmp[1]
            else:
                break
                
    return answer//j_size

운영체제 공부!