Guest User

Untitled

a guest
Jul 16th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Solution
  6. {
  7. const int SECONDS_PER_5_MINUTES = 300;
  8. const int SECONDS_PER_HOUR = 3600;
  9. const int SECONDS_PER_MINUTE = 60;
  10. const int MINUTES_PER_HOUR = 60;
  11.  
  12. public int solution(string S)
  13. {
  14. //Have to separate between time of each call and price of each call, since a call of 7 minutes
  15. //it's not the same as 7 calls of 1 minute. We could have done this with quite a lot of structures,
  16. //I chose dictionaries for simplicity
  17. Dictionary<int, TimeSpan> callsTimeTable = new Dictionary<int, TimeSpan>();
  18. Dictionary<int, long> callsPricingTable = new Dictionary<int, long>();
  19. foreach (string call in S.Split('n'))
  20. {
  21. ProcessCall(call, callsTimeTable, callsPricingTable);
  22. }
  23. //Here, I could do the typical algorithm searching for the max value looping the different timespans
  24. //"if (value > max) max = value"
  25. //Could do the same a few lines below with the Min function
  26. TimeSpan maxTotalCalls = callsTimeTable.Values.Max();
  27.  
  28. List<int> maxTotalCallsNumbers = callsTimeTable.Where(v => v.Value == maxTotalCalls).Select(kv => kv.Key).ToList();
  29. if (maxTotalCallsNumbers.Count == 1)
  30. callsPricingTable.Remove(maxTotalCallsNumbers.First());
  31. else
  32. {
  33. int minPhoneNumber = maxTotalCallsNumbers.Min();
  34. callsPricingTable.Remove(minPhoneNumber);
  35. }
  36. //I assume that, if we have only one number in the dictionary, this has the longest total duration, so it's free.
  37. if (callsPricingTable.Count == 0) return 0;
  38.  
  39. return (int)callsPricingTable.Values.Max();
  40. }
  41.  
  42. private void ProcessCall(string call, Dictionary<int, TimeSpan> callsTimeTable, Dictionary<int, long> callsPricingTable)
  43. {
  44. //dictionaries are reference types, so no ref keyword needed
  45. string[] timeNumber = call.Split(',');
  46. int phoneNumber = Convert.ToInt32(timeNumber[1].Replace("-", ""));
  47. string[] hhmmss = timeNumber[0].Split(':');
  48. TimeSpan actualTimeCall = new TimeSpan(Convert.ToInt32(hhmmss[0]), Convert.ToInt32(hhmmss[1]), Convert.ToInt32(hhmmss[2]));
  49.  
  50. if (!callsTimeTable.ContainsKey(phoneNumber))
  51. {
  52. callsTimeTable.Add(phoneNumber, actualTimeCall);
  53. callsPricingTable.Add(phoneNumber, ProcessCallPricing(actualTimeCall));
  54. }
  55. else
  56. {
  57. TimeSpan allTimeCall = callsTimeTable[phoneNumber];
  58. callsTimeTable[phoneNumber] = allTimeCall.Add(actualTimeCall);
  59. callsPricingTable[phoneNumber] += ProcessCallPricing(actualTimeCall);
  60. }
  61. }
  62.  
  63. private long ProcessCallPricing(TimeSpan callTime)
  64. {
  65. //TimeSpan have a TotalSeconds property, but for the exercise, seconds will be calculated manually
  66. long seconds = callTime.Hours * SECONDS_PER_HOUR + callTime.Minutes * SECONDS_PER_MINUTE + callTime.Seconds;
  67. if (seconds < SECONDS_PER_5_MINUTES)
  68. {
  69. return seconds * 3;
  70. }
  71. return (callTime.Hours * MINUTES_PER_HOUR + callTime.Minutes + (callTime.Seconds > 0 ? 1 : 0)) * 150;
  72. }
  73. }
Add Comment
Please, Sign In to add comment