Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created April 24, 2017 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/e182bbcf49cb73eca1d9bc63815d4e0d to your computer and use it in GitHub Desktop.
Save jianminchen/e182bbcf49cb73eca1d9bc63815d4e0d to your computer and use it in GitHub Desktop.
Email everywhere - score 4.17 from 30 - timeout, no more wrong answers.
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 = "-1";
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