SHARE
TWEET

Untitled

a guest Nov 15th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(S) {
  2.  let total = 0;
  3.  
  4.  const callArr = S.split('\n');
  5.  let callMap = {};
  6.  
  7.  for (let i=0; i<=callArr.length-1; i++) {
  8.  const tempArr = callArr[i].split(',')
  9.  if (!callMap[tempArr[1]]) callMap[tempArr[1]] = { 'cost' : 0, 'duration' : 0 }
  10.  callMap[tempArr[1]].cost = callCost(tempArr[0].split(':'))
  11.  callMap[tempArr[1]].duration += secondsCalled(tempArr[0].split(':')) //duration is in seconds
  12.  }
  13.  
  14.  callMap = longestCall(callMap);
  15.  for (let num in callMap) {
  16.  total += callMap[num].cost;
  17.  }
  18.  
  19.  return total;
  20. }
  21. function callCost(duration) {
  22.  let m = (Number(duration[0])*60) + Number(duration[1]);
  23.  let s = Number(duration[2]);
  24.  
  25.  if (m >= 5) {
  26.  if (s > 0) m++;
  27.  return m*150;
  28.  } else {
  29.  s += m*60
  30.  return s*3
  31.  }
  32. }
  33. function secondsCalled(duration) {
  34.  return (Number(duration[0])*60) + Number(duration[1])*60 + Number(duration[2])
  35. }
  36. function longestCall(callMap) {
  37.  let mostCalled = '';
  38.  let mostSeconds = 0;
  39.  for (let num in callMap) {
  40.  if (callMap[num].duration === mostSeconds) {
  41.  const currNumArr = num.split('-');
  42.  const currVal = Number(currNumArr[0]+currNumArr[1]+currNumArr[2]);
  43.  
  44.  const mostArr = num.split('-');
  45.  const mostVal = Number(mostArr[0]+mostArr[1]+mostArr[2]);
  46.  
  47.  // What if the numerical values of the phone numbers are equal? No details in spec for that case...
  48.  if (currVal < mostVal) mostCalled = num;
  49.  }
  50.  if (callMap[num].duration > mostSeconds) {
  51.  mostSeconds = callMap[num].duration;
  52.  mostCalled = num;
  53.  }
  54.  }
  55.  
  56.  delete callMap[mostCalled];
  57.  return callMap;
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top