Guest User

Untitled

a guest
Jan 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Arrays;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6.  
  7. class Solution {
  8.  
  9.  
  10. /**
  11. * Solutions
  12. * @param S
  13. * @return
  14. */
  15. public int solution(String S) {
  16.  
  17. int total = 0, max = 0;
  18. int sec;
  19.  
  20. String[] lines = S.split("\n");
  21.  
  22. HashMap<String, Integer> phonesTime = new HashMap<>();
  23.  
  24. Arrays.stream(lines).map(s -> s.split(",")).forEach(line -> {
  25. String phone = line[1];
  26. int time = timeToSecs(line[0]);
  27.  
  28.  
  29. //Sum up same phone number into one
  30. if (phonesTime.containsKey(phone)) {
  31. int oldTime = phonesTime.get(phone);
  32. phonesTime.put(phone, oldTime + time);
  33. } else {
  34. phonesTime.put(phone, time);
  35. }
  36. });
  37.  
  38. for (String key : phonesTime.keySet()) {
  39. sec = phonesTime.get(key);
  40. if (sec >= max) {
  41. max = sec;
  42. }
  43. total += secsToCents(sec);
  44. }
  45.  
  46.  
  47.  
  48. //remove promotion rebate
  49. total -= secsToCents(max);
  50.  
  51. return total;
  52. }
  53.  
  54. /**
  55. * Business logic
  56. * @param sec
  57. * @return
  58. */
  59. private static int secsToCents(int sec) {
  60.  
  61. if (sec < 300) return sec * 3;
  62. else if (sec % 60 == 0) return (sec / 60) * 150;
  63. else return ((sec / 60) + 1) * 150;
  64.  
  65. }
  66.  
  67. /**
  68. * timeToSecs
  69. * @param time
  70. * @return
  71. */
  72. private static int timeToSecs(String time) {
  73. DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
  74. Date date = null;
  75. Date reference = null;
  76. try {
  77. reference = dateFormat.parse("00:00:00");
  78. date = dateFormat.parse(time);
  79. }catch ( Exception ex){
  80.  
  81. }
  82. long seconds = (date.getTime() - reference.getTime()) / 1000L;
  83. return Math.toIntExact(seconds);
  84.  
  85. }
  86.  
  87. public static void main(String[] args) {
  88. String S = "00:01:07,400-234-090\n" +
  89. "00:05:01,701-080-080\n" +
  90. "00:05:00,400-234-090";
  91. Solution s = new Solution();
  92.  
  93. int solution = s.solution(S);
  94.  
  95. System.out.println(solution);
  96. }
  97.  
  98.  
  99. }
Add Comment
Please, Sign In to add comment