글 작성자: Doublsb

https://programmers.co.kr/learn/courses/30/lessons/42587

 

풀이

- 중요도 높은 것 먼저 인쇄

- 대기목록 가장 앞에 있는 문서 꺼내기

- 그러나 중요도가 그거보다 높은 문서가 리스트에 있다?

- 그러면 고걸 다시 대기목록에 집어넣음

- 내가 인쇄 요청한 문서가 몇번째에 인쇄되는지 return 

 

- 관건은 중요도가 같은 경우로군 오케이

- 문제에서 사실상 구현 방법을 정해줬으니 그대로 해 보자

 

C#

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {
    public int solution(int[] priorities, int location) {
        
        var WaitList = new WaitList(priorities, location);
        
        Document Result = null;
        int TryCount = 0;
        while(Result == null || Result.isTarget == false) { 
            Result = WaitList.Get();
            TryCount++;
        }

        return TryCount;
    }
}

public class WaitList {
    private List<Document> List = new List<Document>();
    
    public WaitList(int[] sheets, int location) {
        for(int i=0; i<sheets.Length; i++) {
            List.Add(new Document(sheets[i], i == location ? true : false));
        }
    }
    
    public Document Get() {
        Document Result = null;
        int max = List.Max(e => e.Priority);
        
        while(Result == null) {
            var popped = List[0];
            
            if (popped.Priority == max) {
                Result = popped;
                List.RemoveAt(0);
            }
            else {
                List.RemoveAt(0);
                List.Add(popped);
            }
        }
        
        return Result;
    }
}

public class Document {
    public bool isTarget;
    public int Priority;
    
    public Document(int priority, bool isTarget) {
        this.Priority = priority;
        this.isTarget = isTarget;
    }
}

 

결과

100.0 / 100.0

 

피드백

하라는 대로 구현하면 되는 문제라서 특별한 건 없었다.

반응형