Created
April 24, 2017 04:04
-
-
Save jianminchen/e182bbcf49cb73eca1d9bc63815d4e0d to your computer and use it in GitHub Desktop.
Email everywhere - score 4.17 from 30 - timeout, no more 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 = "-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