Created
April 24, 2017 04:06
-
-
Save jianminchen/09a7ad058f13179934bd0e9a8008cc46 to your computer and use it in GitHub Desktop.
Email everywhere - score 19 maximum score 30
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) | |
{ | |
ProcessInput(); | |
//RunTestcase(); | |
} | |
public static void RunTestcase() | |
{ | |
var queries = 3; | |
const int MAX = 100000; | |
var queueByPriority = new Queue<string>[MAX]; // priority - 1 | |
int highestPriority = -1; | |
for (int i = 0; i < MAX; i++) | |
{ | |
queueByPriority[i] = new Queue<string>(); | |
} | |
var messages = new string[] { "store email1 1", "store email2 10", "get_next_email" }; | |
IList<string> result = new List<string>(); | |
for (int i = 0; i < queries; i++) | |
{ | |
var command = messages[i].Split(' '); | |
if (command.Length == 1) | |
{ | |
// get next email | |
result.Add( RemoveFirstEmail(queueByPriority, MAX, ref highestPriority)); | |
} | |
else | |
{ | |
var message = command[1]; | |
var priority = command[2]; | |
SaveMessageToQueue(queueByPriority, message, priority, ref highestPriority); | |
} | |
} | |
} | |
public static void ProcessInput() | |
{ | |
var queries = Convert.ToInt32(Console.ReadLine()); | |
const int MAX = 100000; | |
var queueByPriority = new Queue<string>[MAX]; // priority - 1 | |
int highestPriority = -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, ref highestPriority)); | |
} | |
else | |
{ | |
var message = command[1]; | |
var priority = command[2]; | |
SaveMessageToQueue(queueByPriority, message, priority, ref highestPriority); | |
} | |
} | |
} | |
/// <summary> | |
/// save - O(1), easy to find the queue, which queue to save - by priority number | |
/// </summary> | |
/// <param name="queueByPriority"></param> | |
/// <param name="message"></param> | |
/// <param name="priority"></param> | |
public static void SaveMessageToQueue(Queue<string>[] queueByPriority, string message, string priority, ref int highestPriority) | |
{ | |
int index = Convert.ToInt32(priority); | |
queueByPriority[index - 1].Enqueue(message); | |
var newPriority = index - 1; | |
if (newPriority > highestPriority) | |
{ | |
highestPriority = newPriority; | |
} | |
} | |
/// <summary> | |
/// size is 100000 - need to do time complexity analysis | |
/// it is O(n) algorithm to remove, not efficient | |
/// </summary> | |
/// <param name="queueByPriority"></param> | |
/// <param name="size"></param> | |
/// <returns></returns> | |
public static string RemoveFirstEmail(Queue<string>[] queueByPriority, int size, ref int highestPriority) | |
{ | |
var message = "-1"; | |
if(highestPriority < 0 && queueByPriority[highestPriority].Count == 0) | |
{ | |
return message; | |
} | |
Queue<string> current = queueByPriority[highestPriority]; | |
message = current.Dequeue(); | |
if(current.Count == 0) | |
{ | |
FindNextHighestPriority(queueByPriority, ref highestPriority); | |
} | |
return message; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="queueByPriority"></param> | |
/// <param name="highestPriority"></param> | |
public static void FindNextHighestPriority(Queue<string>[] queueByPriority, ref int highestPriority) | |
{ | |
int start = highestPriority; | |
for(int i = start -1; i >= 0; i--) | |
{ | |
if (queueByPriority[i].Count == 0) | |
{ | |
continue; | |
} | |
highestPriority = i; // found the one | |
return; | |
} | |
highestPriority = -1; // not found | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment