-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0539-minimum-time-difference.js
More file actions
34 lines (29 loc) · 1.12 KB
/
0539-minimum-time-difference.js
File metadata and controls
34 lines (29 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Minimum Time Difference
* Time Complexity: O(N log N)
* Space Complexity: O(N)
*/
var findMinDifference = function (timePoints) {
const parsedTimeInMinutes = timePoints.map((currentPoint) => {
const timeFragments = currentPoint.split(":");
const hourFragment = Number(timeFragments[0]);
const minuteFragment = Number(timeFragments[1]);
return hourFragment * 60 + minuteFragment;
});
parsedTimeInMinutes.sort((valueOne, valueTwo) => valueOne - valueTwo);
let minimumDifferenceResult = Infinity;
for (let timeIndex = 1; timeIndex < parsedTimeInMinutes.length; timeIndex++) {
const currentDifference =
parsedTimeInMinutes[timeIndex] - parsedTimeInMinutes[timeIndex - 1];
minimumDifferenceResult = Math.min(
minimumDifferenceResult,
currentDifference,
);
}
const totalMinutesInDay = 24 * 60;
const lastTimeValue = parsedTimeInMinutes[parsedTimeInMinutes.length - 1];
const firstTimeValue = parsedTimeInMinutes[0];
const wrappedAroundDifference =
totalMinutesInDay - lastTimeValue + firstTimeValue;
return Math.min(minimumDifferenceResult, wrappedAroundDifference);
};