查找最接近给定24小时时间的时间

时间:2018-07-12 20:39:01

标签: javascript time

给出函数签名:

getNearestToDesiredTime(left, middle, right, desiredTime)

其中:

  • left, middle, right可能是有效的24小时制(1830, -1, 1900
  • desiredTime是期​​望的24小时制(1845

您如何找到最接近所需时间的时间?

在时间参数都与所需时间等距的情况下,应返回较晚的时间。 (在上面的示例中,应该返回1900,因为19001830都离期望的15 1845分钟)。

到目前为止,这是我所拥有的,但似乎不太奏效。

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;
};

测试用例:

// 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))

2 个答案:

答案 0 :(得分:0)

对其稍作更改以处理一系列时间,以使其更加通用。我们会跟踪最佳差异,然后返回产生差异的时间

// 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];
}

答案 1 :(得分:0)

此解决方案使用其他方法。它计算所有三个时间戳从午夜开始的分钟数,然后使用leftright时间戳相对于desired时间戳的时间差来确定最接近的时间戳。

我用很多时间戳对其进行了测试。还有一些奇怪的。到目前为止,对于所有人似乎都可以正常工作。

希望这会有所帮助。

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');