Last active
April 13, 2024 22:24
-
-
Save osahondev/2b985fdfdc06e18ef6ff261155fb394e to your computer and use it in GitHub Desktop.
Time Management Implementation to help Esther manage her time effectively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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..