프로그래머스 | 프린터 | 큐(Queue)

Algorithm 2019. 8. 29. 09:39
반응형
  • 프린터
  • darklight

    sublimevimemacs

    C# 

문제 설명

일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린터를 개발했습니다. 이 새롭게 개발한 프린터는 아래와 같은 방식으로 인쇄 작업을 수행합니다.

1. 인쇄 대기목록의 가장 앞에 있는 문서(J)를 대기목록에서 꺼냅니다. 2. 나머지 인쇄 대기목록에서 J보다 중요도가 높은 문서가 한 개라도 존재하면 J를 대기목록의 가장 마지막에 넣습니다. 3. 그렇지 않으면 J를 인쇄합니다.

예를 들어, 4개의 문서(A, B, C, D)가 순서대로 인쇄 대기목록에 있고 중요도가 2 1 3 2 라면 C D A B 순으로 인쇄하게 됩니다.

내가 인쇄를 요청한 문서가 몇 번째로 인쇄되는지 알고 싶습니다. 위의 예에서 C는 1번째로, A는 3번째로 인쇄됩니다.

현재 대기목록에 있는 문서의 중요도가 순서대로 담긴 배열 priorities와 내가 인쇄를 요청한 문서가 현재 대기목록의 어떤 위치에 있는지를 알려주는 location이 매개변수로 주어질 때, 내가 인쇄를 요청한 문서가 몇 번째로 인쇄되는지 return 하도록 solution 함수를 작성해주세요.

제한사항

  • 현재 대기목록에는 1개 이상 100개 이하의 문서가 있습니다.
  • 인쇄 작업의 중요도는 1~9로 표현하며 숫자가 클수록 중요하다는 뜻입니다.
  • location은 0 이상 (현재 대기목록에 있는 작업 수 - 1) 이하의 값을 가지며 대기목록의 가장 앞에 있으면 0, 두 번째에 있으면 1로 표현합니다.

입출력 예

prioritieslocationreturn

[2, 1, 3, 2] 2 1
[1, 1, 9, 1, 1, 1] 0 5

입출력 예 설명

예제 #1

문제에 나온 예와 같습니다.

예제 #2

6개의 문서(A, B, C, D, E, F)가 인쇄 대기목록에 있고 중요도가 1 1 9 1 1 1 이므로 C D E F A B 순으로 인쇄합니다.

출처

 

1
2
3
4
5
6
7
8
using System;
 
public class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        return answer;
    }
}
 
 

 

 

 

내풀이 

예제 1: 내가 요청한 문서 B 

결과: 4번째 출력 

실패 !

 

정담: 1번째 출력... 왜?

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace Application
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var sol = new Solution();
 
            int[] priorities = { 2132 };
            int location = 2;
 
            //int[] priorities = { 1, 1, 9, 1, 1, 1 };
            //int location = 0;
 
            var result = sol.solution(priorities, location);
            Console.WriteLine("result: " + result);
        }
    }
 
    public class Solution
    {
        private class Doc {
            public object Data { get; private set; }
            public int Piriority { get; set; }
 
            public Doc(object data) {
                this.Data = data;
            }
        }
 
        public int solution(int[] priorities, int location)
        {
            int answer = 0;
            List<Doc> docList = new List<Doc>();
 
            //test
            string[] arr = { "A""B""C""D""E""F" };
            //string[] arr = { "A", "B", "C", "D" };
 
            Doc target = null;
 
            //대기열과 중요도 넣기 
            for (int i = 0; i < priorities.Length; i++) {
                docList.Add(new Doc(arr[i]) { Piriority = priorities[i] });
            }
 
            //타겟 설정
            location = (location - 1>= 0 ? location - 1 : 0;
            target = docList[location];
            Console.WriteLine("target: {0}"target.Data);
 
            //중요도에 따라 내림차순 정렬
            int n = 1;  //순번 
            Queue<Doc> q = new Queue<Doc>();
            while (docList.Count > 0) {
                var doc = docList.FirstOrDefault();
                docList.Remove(doc);
                if (docList.Any(x => x.Piriority > doc.Piriority))
                {
                    docList.Add(doc);
                }
                else {
                    q.Enqueue(doc);
                    Console.WriteLine("{0} -> {1}:{2}", n, doc.Data, doc.Piriority);
                    if(target.Data == doc.Data)
                    {
                        answer = n;   
                    }
                    n++;
                }
            }
 
            var e = q.GetEnumerator();
            
            
            //while (e.MoveNext()) {
            //    var doc = e.Current;
 
            //    Console.WriteLine("{0} -> {1}:{2}", idx, doc.Data, doc.Piriority);
 
            //    //if (doc.Data.Equals(target.Data)) {
            //    //    answer = idx + 1;
            //    //    //break;
            //    //}
                
            //}
 
            return answer;
        }
    }
}
 
 
 

 

 

 

다른사람 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 
namespace Application
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            
            var s = new Solution();
            int[] priorities = { 2132 };
            int location = 2;
            var result = s.solution(priorities, location);
            Console.WriteLine("result: {0}", result);
        }
    }
 
    public class Solution
    {
        public int solution(int[] priorities, int location)
        {
            int answer = 0;
            Queue<KeyValuePair<intint>> que = new Queue<KeyValuePair<intint>>();
            for (int i = 0; i < priorities.Length; i++)
            {
                que.Enqueue(new KeyValuePair<intint>(i, priorities[i]));
            }
            while (true)
            {
                int nMax = que.Max(x => x.Value);
                var kv = que.Dequeue();
                if (kv.Value == nMax)
                {
                    if (kv.Key == location) return answer + 1;
                    else
                    {
                        answer++;
                        continue;
                    }
                }
                que.Enqueue(kv);
            }
        }
    }
}
 
 
 
반응형
: