• API
• FAQ
• Tools
• Archive
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]) callMap[tempArr] = { 'cost' : 0, 'duration' : 0 }
10.  callMap[tempArr].cost = callCost(tempArr.split(':'))
11.  callMap[tempArr].duration += secondsCalled(tempArr.split(':')) //duration is in seconds
12.  }
13.
14.  callMap = longestCall(callMap);
15.  for (let num in callMap) {
16.  total += callMap[num].cost;
17.  }
18.
20. }
21. function callCost(duration) {
22.  let m = (Number(duration)*60) + Number(duration);
23.  let s = Number(duration);
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)*60) + Number(duration)*60 + Number(duration)
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+currNumArr+currNumArr);
43.
44.  const mostArr = num.split('-');
45.  const mostVal = Number(mostArr+mostArr+mostArr);
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.

Top