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
@meekg33k
Copy link

Hello @enaiho, thank you for participating in Week 7 of #AlgorithmFridays.

Your solution to help Esther get better at time management works for the most part. But then it fails some important test cases, here is an example of one of such test cases:

  • If Esther recorded the following times ["16:15", "16:00", "12:20", "16:01"], your solution would return 14 minutes as the minimum number of minutes instead of 1. minTimeDifference(["16:15", "16:00", "12:20", "16:01"]); //❌ returns 14 instead of 1

What do you think is wrong with your solution's logic? I'd love to hear you thoughts.

@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