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
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
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:
["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 1What do you think is wrong with your solution's logic? I'd love to hear you thoughts.