코딩공부/프로그래머스

[프로그래머스]Lv.2 프로세스C#

usingsystem 2023. 8. 1. 13:35
728x90

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

소스코드

using System;
using System.Linq;
using System.Collections.Generic;
public class Solution
{
    public int solution(int[] priorities, int location)
    {
        int answer = 0;

        Queue<(int, int)> q = new Queue<(int, int)>();
        for (int i = 0; i < priorities.Length; i++)
            q.Enqueue((i, priorities[i]));

        Queue<int> maxs = new Queue<int>();
        var dis = priorities.OrderByDescending(o => o).ToArray();
        for (int i = 0; i < dis.Length; i++)
            maxs.Enqueue(dis[i]);

        int max = maxs.Dequeue();
        while (q.Count() > 0)
        {
            var item = q.Dequeue();

            if (item.Item2 < max)
            {
                q.Enqueue(item);
            }
            else if (item.Item1 == location)
            {
                answer++;
                break;
            }
            else
            {
                answer++;
            }
            if (item.Item2 == max)
                max = maxs.Dequeue();
        }
        return answer;
    }
}
728x90