Skip to content

Instantly share code, notes, and snippets.

@osahondev
Last active April 13, 2024 22:24
Show Gist options
  • Select an option

  • Save osahondev/2b985fdfdc06e18ef6ff261155fb394e to your computer and use it in GitHub Desktop.

Select an option

Save osahondev/2b985fdfdc06e18ef6ff261155fb394e to your computer and use it in GitHub Desktop.
Time Management Implementation to help Esther manage her time effectively
const convertToMinutes = (time) => {
let [hr,minute] = time.split(":");
if( Number.parseInt(hr) > 23 || Number.parseInt(minute) > 59) return 0;
return ( Number.parseInt(hr)*60)+ Number.parseInt(minute);
}
const minimumTimeDifference = ( times ) =>{
if(!Array.isArray(times) ) return 0;
if( times.length == 0 ) return 0;
for( let [index,time] of times.entries() ){
times[index] = convertToMinutes(time);
}
times.sort( (a,b)=>a-b );
if( times.length == 1 ) return times[0];
let timeDifference = Infinity;
for(let i=0; i<times.length-1; i++){
if( times[i+1]-times[i] < timeDifference )
timeDifference = times[i+1]-times[i];
}
return timeDifference;
}
minimumTimeDifference(["16:15","16:00","12:20"]); // sample input
@osahondev
Copy link
Author

Hello @meekg33k. Thanks for spotting this. I just discovered that a 2 pointer approach at both ends was perhaps an overthought to the solution. I have refactored that bit of the logic to do a (next-previous). I believe this should be more correct.

Thanks..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment