Find the closest time to a given 24-hour time

问题: Given the function signature: getNearestToDesiredTime(left, middle, right, desiredTime) where: left, middle, right are possibly-valid 24-hour times (1830, -1, 190...

问题:

Given the function signature:

getNearestToDesiredTime(left, middle, right, desiredTime)

where:

  • left, middle, right are possibly-valid 24-hour times (1830, -1, 1900)
  • desiredTime is a desired 24-hour time (1845)

how would you find the time CLOSEST to the desired time?

In a situation where the time arguments are all equidistant to the desired time, the later time should be returned. (in the above example, 1900 should be returned, because 1900 and 1830 are both 15 minutes from the desired 1845).

This is what I have so far but it doesn't seem to quite work.

const timeDiffInMinutes = (t1, t2) => {
  const t1Mins = t1 / 100 * 60 + t1 % 100;
  const t2Mins = t2 / 100 * 60 + t2 % 100;
  return t2Mins - t1Mins;
};

const getNearestToDesiredTime = (left, middle, right, desiredTime) => {
  if (middle !== -1) {
    return middle;
  } else if (left === -1 && right === -1) {
    return null;
  }

  // result should be whatever time is _closest_ to the desiredTime.
  // if both options are equi-distant to the desired time, show the later time.
  const leftIsCloser =
    timeDiffInMinutes(desiredTime, left) <
    timeDiffInMinutes(desiredTime, right);

  return leftIsCloser ? left : right;
};

Test Cases:

// this should be 1900
console.log(getNearestToDesiredTime(1830, 1900, 1945, 1900))

// this should be 1915
console.log(getNearestToDesiredTime(1830, 1915, 1945, 1900))

// this should be 1830
console.log(getNearestToDesiredTime(1830, -1, 1945, 1845))

// this should be 1945
console.log(getNearestToDesiredTime(1830, -1, 1945, 1930))

// this should be 1900
console.log(getNearestToDesiredTime(1830, -1, 1900, 1845))

回答1:

This solution uses a different approach. It calculates the minutes from midnight for all three timestamps and then uses the difference between the left and right timestamp in relation to the desired timestamp to determine which is nearest.

I tested it with many timestamps. Also some odd ones. Seems to work fine so far for all.

Hope this helps.

const minutesFromMidnight = (t) => {
  const frac = t / 100;
  const hours = Math.trunc(frac);
  const mins = (frac - hours) * 100;
  const minsOfDay = (hours * 60) + mins;

  var minutesDist = 0;
  if (minsOfDay >= 720)
    minutesDist = minsOfDay - 1440;
  else
    minutesDist = minsOfDay;

  return minutesDist;
};

const getNearestToDesiredTime = (left, middle, right, desiredTime) => {
  if (middle !== -1) {
    return middle;
  } else if (left === -1 && right === -1) {
    return null;
  }

  const desiredDist = minutesFromMidnight(desiredTime);
  const leftDist = minutesFromMidnight(left);
  const rightDist = minutesFromMidnight(right);

  const leftDiff = Math.abs(leftDist - desiredDist);
  const rightDiff = Math.abs(rightDist - desiredDist);

  const leftIsCloser = leftDiff < rightDiff;

  return leftIsCloser ? left : right;
};

// this should be 1830
console.log(getNearestToDesiredTime(1830, -1, 1945, 1845) + ' = 1830');

// this should be 1945
console.log(getNearestToDesiredTime(1830, -1, 1945, 1930) + ' = 1945');

// this should be 1900
console.log(getNearestToDesiredTime(1830, -1, 1900, 1845) + ' = 1900');

// this should be 1800
console.log(getNearestToDesiredTime(1800, -1, 1900, 1745) + ' = 1800');

// this should be 1900
console.log(getNearestToDesiredTime(130, -1, 1900, 1600) + ' = 1900');

// this should be 130
console.log(getNearestToDesiredTime(1900, -1, 130, 2300) + ' = 130');

// this should be 2315
console.log(getNearestToDesiredTime(130, -1, 2315, 0) + ' = 2315');

// this should be 130
console.log(getNearestToDesiredTime(130, -1, 2315, 100) + ' = 130');


回答2:

Changed it slightly to handle an array of times to be more generic. We keep track of the best difference and then return the time that made it

// this should be 1900
console.log(getNearestToDesiredTime([1830, 1900, 1945], 1900))

// this should be 1915
console.log(getNearestToDesiredTime([1830, 1915, 1945], 1900))

// this should be 1830
console.log(getNearestToDesiredTime([1830, 1945], 1845))

// this should be 1945
console.log(getNearestToDesiredTime([1830, 1945], 1930))

// this should be 1900
console.log(getNearestToDesiredTime([1830, 1900], 1845))

function timeDiffInMinutes (t1, t2) {
  const t1Mins = Math.floor(t1 / 100) * 60 + t1 % 100;
  const t2Mins = Math.floor(t2 / 100) * 60 + t2 % 100;
  return t2Mins - t1Mins;
};


function getNearestToDesiredTime (times, target) {
  var best = Infinity;
  var bestIndex = -1;
  
  for (var i = 0; i < times.length; i++) {
      var difference = Math.abs(timeDiffInMinutes(times[i], target));
      if (difference <= best) {
          best = difference;
          bestIndex = i;
      }
  }
  return times[bestIndex];
}

  • 发表于 2018-07-14 00:50
  • 阅读 ( 249 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除