Created
September 20, 2016 04:58
-
-
Save jianminchen/e0a94f4f3b9432a81b51b68951647a8a to your computer and use it in GitHub Desktop.
HackerRank - Stryker code sprint - study code #6
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.IO; | |
using System.Linq; | |
class Solution { | |
const string PointNotFoundText = "Point doesn't exist in the bucket."; | |
class Point { | |
public int ID { get; set; } | |
public decimal X { get; set; } | |
public decimal Y { get; set; } | |
public decimal Z { get; set; } | |
public override string ToString() { | |
return string.Format("{0} = ({1:F3},{2:F3},{3:F3})", ID, X, Y, Z); | |
} | |
} | |
static Point CreatePoint(string[] vals) { | |
return new Point { | |
ID = int.Parse(vals[0]), | |
X = decimal.Parse(vals[1]), | |
Y = decimal.Parse(vals[2]), | |
Z = decimal.Parse(vals[3]) | |
}; | |
} | |
static Point FindPoint(IDictionary<int, Point> bucket, int k) { | |
return bucket.ContainsKey(k) ? bucket[k] : null; | |
} | |
static bool RemovePoint(IDictionary<int, Point> bucket, Queue<Point> list, int k) { | |
if (list.Count == 0) | |
return false; | |
if (bucket.ContainsKey(k)) | |
bucket.Remove(k); | |
Point p = list.Dequeue(); | |
bucket[p.ID] = p; | |
return true; | |
} | |
static void Main(String[] args) { | |
int[] nb = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); | |
int n = nb[0], b = nb[1]; | |
var bucket = new Dictionary<int, Point>(b); | |
var list = new Queue<Point>(n); | |
// read points | |
for (int i=0; i<n; i++) { | |
list.Enqueue(CreatePoint(Console.ReadLine().Split(' '))); | |
} | |
// sort by Z descending | |
list = new Queue<Point>(list.OrderByDescending(p => p.Z)); | |
// fill bucket | |
for (int i=0; i<b; i++) { | |
Point p = list.Dequeue(); | |
bucket[p.ID] = p; | |
} | |
while (true) { | |
string qs = Console.ReadLine(); | |
if (string.IsNullOrWhiteSpace(qs)) | |
break; | |
string[] q = qs.Split(' '); | |
int k = int.Parse(q[1]); | |
switch (q[0][0]) { | |
case 'F': | |
case 'f': | |
Point p = FindPoint(bucket, k); | |
Console.WriteLine(p != null ? p.ToString() : PointNotFoundText); | |
break; | |
case 'R': | |
case 'r': | |
Console.WriteLine(!bucket.ContainsKey(k) ? PointNotFoundText : RemovePoint(bucket, list, k) ? string.Format("Point id {0} removed.", k) : "No more points can be deleted."); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment