My idea is different, hope this helps someone. My solution does use extra space. it uses a min heap to sort it in nlogn time after fixing the edge case. The way to beat the issue of the time going past midnight is to just add 1440 minutes or 24 hours to each timePoints as a new point. If you think about it, 00:00 is no different from 24:00, so we're just adding the extra day to fix our edge case issue. For example, if the test case is [00:00, 23:59], the list is now [00:00,23:59, 24:00, 47:59] in a min heap. Then you can easily just convert it to all to minutes. Then in a simple while loop, pop from head, let's call it a, and do an lowest = min(lowest, abs(a - minHeap[0])) and watch out for the edge case of when minHeap is length of 1 as it doesn't have anything to compare if it's the last item in the heap.
There is one missing point of using the second approach. In the line 23 we have to iterate till the `last_m` (max possible minute from the line 15) instead of `len(exists)`. It will improve efficiency for some test cases.
Thanks for your video! Here is a solution in one line: def findMinDifference(self, a): return min(map(sub,(q:=sorted(int(s[:2])*60+int(s[3:]) for s in a))[1:]+[q[0]+1440],q))
As always Awesome solution. Last year had interview with DocuSign and I could not clear that round , all because i used built in sorting , but they were expecting counting Sort.
if timePoints.count(min(timePoints)) > 1: return 0 for i in range(len(timePoints)): h,m = map(int,timePoints[i].split(":")) timePoints[i] = m+(h*60) timePoints.sort() res = 1440 - timePoints[-1] + timePoints[0] for i in range(1, len(timePoints)): diff = timePoints[i]-timePoints[i-1] res = min(res, diff) return res
So for the second part, you are not computing the two diffs for each set of time points correct? You said you would in the explanation but I dont think this approach does unless I'm wrong
My idea is different, hope this helps someone. My solution does use extra space. it uses a min heap to sort it in nlogn time after fixing the edge case. The way to beat the issue of the time going past midnight is to just add 1440 minutes or 24 hours to each timePoints as a new point. If you think about it, 00:00 is no different from 24:00, so we're just adding the extra day to fix our edge case issue. For example, if the test case is [00:00, 23:59], the list is now [00:00,23:59, 24:00, 47:59] in a min heap. Then you can easily just convert it to all to minutes. Then in a simple while loop, pop from head, let's call it a, and do an lowest = min(lowest, abs(a - minHeap[0])) and watch out for the edge case of when minHeap is length of 1 as it doesn't have anything to compare if it's the last item in the heap.
There is one missing point of using the second approach.
In the line 23 we have to iterate till the `last_m` (max possible minute from the line 15) instead of `len(exists)`. It will improve efficiency for some test cases.
Oh good point 💡
Thanks for your video!
Here is a solution in one line:
def findMinDifference(self, a):
return min(map(sub,(q:=sorted(int(s[:2])*60+int(s[3:]) for s in a))[1:]+[q[0]+1440],q))
yep good luck using this in an interview
That solution is missing the fact that there will be no bigger distance than 12h, so half of 1440
good point!
technically 1440 is still a more precise upperbound than float("inf"), but you're write, 720 should be sufficient as well
@@NeetCodeIO print(f"{write}")
As always Awesome solution.
Last year had interview with DocuSign and I could not clear that round , all because i used built in sorting , but they were expecting counting Sort.
Oh counting sort!
note that the min part only works when the time is sorted, ty for the great solution
My JS Solution beats 97% Thank You NeetCode :)
if timePoints.count(min(timePoints)) > 1:
return 0
for i in range(len(timePoints)):
h,m = map(int,timePoints[i].split(":"))
timePoints[i] = m+(h*60)
timePoints.sort()
res = 1440 - timePoints[-1] + timePoints[0]
for i in range(1, len(timePoints)):
diff = timePoints[i]-timePoints[i-1]
res = min(res, diff)
return res
thank you bro
The part you initialize the res was mind blowing, gotta watch daily problem everyday no matter whether I can solve the question✨
Like if you love neetcode!
Off course 😂
downvote if you don't like people begging for likes.
So for the second part, you are not computing the two diffs for each set of time points correct? You said you would in the explanation but I dont think this approach does unless I'm wrong
Neetcode make a video on sieve of eratosthenes please
Avg neetcode W