Minimum Time Difference LeetCode solution in JavaScript

1
var findMinDifference = function (timePoints) {
2
const DAY_MINUTES = 24 * 60;
3
const set = new Set();
4

5
for (let index = 0; index < timePoints.length; index++) {
6
const time = timePoints[index];
7
const [hours, minutes] = time.split(":");
8
const totalMinutes = hours * 60 + +minutes;
9
if (set.has(totalMinutes)) return 0;
10
set.add(totalMinutes);
11
}
12

13
const timeMinutes = [...set].sort((a, b) => a - b);
14
return timeMinutes.reduce((min, time, index) => {
15
const next = timeMinutes[index + 1];
16
const diff = next ? next - time : DAY_MINUTES - time + timeMinutes[0];
17
return Math.min(min, diff);
18
}, Infinity);
19
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0