https://school.programmers.co.kr/learn/courses/30/lessons/42587?language=csharp

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

using System;
using System.Collections.Generic;
public class Solution {
    public int solution(int[] priorities, int location) {
            Queue<(int priority, int index)> q = new Queue<(int, int)>();

            for (int i = 0; i < priorities.Length; i++) 
            {
                q.Enqueue((priorities[i], i));
            }

            int count = 0;

            while (q.Count > 0) 
            {
                var cur = q.Dequeue();

                bool isFind = false;

                foreach (var item in q) 
                {
                    if (cur.priority < item.priority)
                    {
                        isFind = true;
                        break;
                    }
                }

                if (isFind)
                {
                    q.Enqueue(cur);
                }
                else
                {
                    count++;
                    if(cur.index == location)
                        return count;
                }
            }

            return count;
    }
}

 

 

큐를 이용한 시물레이션 문제로,

1. 큐에는 원래 위치와, 우선 순위를 저장한다.

2. 맨앞으로 꺼낸뒤 그 뒤에 우선순위가 더 높은 값이 있는지 찾아본다

3. 우선순위가 높은 문서가 있다면 다시 큐에 넣는다.

4. 없다면 꺼낸 갯수를 증가 시킨다.

5. 위 과정을 큐가 0이 될때까지 반복한다. 

+ Recent posts