반복횟수가 몇백만 되니까 최대한 줄이려고 해도 시간초과가 나는데 StringBuilder써야 겨우 합격하고 문제 의도하고 맞는가? 아래는 큐 구현하는 코드.

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Queue_Ints queue = new Queue_Ints(); StringBuilder sb = new StringBuilder(); int num_case = int.Parse(Console.ReadLine()); string[] input; for (int i = 0; i < num_case; i++) { input = Console.ReadLine().Split(); if (input[0] == "push") { queue.Push(int.Parse(input[1])); } if (input[0] == "pop") { sb.AppendLine(queue.Pop().ToString()); } if (input[0] == "size") { sb.AppendLine(queue.size().ToString()); } if (input[0] == "empty") { sb.AppendLine(queue.Empty().ToString()); } if (input[0] == "front") { sb.AppendLine(queue.Front().ToString()); } if (input[0] == "back") { sb.AppendLine(queue.Back().ToString()); } } Console.Write(sb); } class Queue_Ints { private LinkedList<int> data = new LinkedList<int>(); public void Push(int N) { data.AddLast(N); } public int Pop() { if (data.Count == 0) { return -1; } else { int item = data.First.Value; data.RemoveFirst(); return item; } } public int size() { return data.Count; } public int Empty() { if (data.Count == 0) { return 1; } else { return 0; } } public int Front() { if (data.Count == 0) { return -1; } else { return data.First.Value; } } public int Back() { if (data.Count == 0) { return -1; } else { return data.Last.Value; } } } } | cs |
