Created
April 24, 2017 04:02
-
-
Save jianminchen/b103a36489d08a130d425f9f5ecdf0ea to your computer and use it in GitHub Desktop.
Emails everywhere - score 2.5 from 30 - pass 4 test cases, timeout and wrong answers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Email | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var queries = Convert.ToInt32(Console.ReadLine()); | |
const int MAX = 100000; | |
var queueByPriority = new Queue<string>[MAX]; // priority - 1 | |
for (int i = 0; i < MAX; i++ ) | |
{ | |
queueByPriority[i] = new Queue<string>(); | |
} | |
for (int i = 0; i < queries; i++) | |
{ | |
var command = Console.ReadLine().Split(' '); | |
if (command.Length == 1) | |
{ | |
// get next email | |
Console.WriteLine(RemoveFirstEmail(queueByPriority, MAX)); | |
} | |
else | |
{ | |
var message = command[1]; | |
var priority = command[2]; | |
SaveMessageToQueue(queueByPriority, message, priority); | |
} | |
} | |
} | |
public static void SaveMessageToQueue(Queue<string>[] queueByPriority, string message, string priority) | |
{ | |
int index = Convert.ToInt32(priority); | |
queueByPriority[index - 1].Enqueue(message); | |
} | |
public static string RemoveFirstEmail(Queue<string>[] queueByPriority, int size) | |
{ | |
var message = string.Empty; | |
for(int i = size - 1; i >= 0; i--) | |
{ | |
var current = queueByPriority[i]; | |
if(current.Count == 0) | |
{ | |
continue; | |
} | |
message = current.Dequeue(); | |
break; | |
} | |
return message; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment