EMPLOYEE FREE TIME (Leetcode) - Code & Whiteboard

  Рет қаралды 9,596

babybear4812

babybear4812

Күн бұрын

Time Complexity: O(NlogN), where N is the number of intervals (this is because of the sort on line 22).
Space Complexity: O(N) because of the intervals list being created!
Leetcode #759 - Employee Free Time, with an easy-to-understand explanation :)
Let me know if you have any questions down below, or if there's a question you'd like me to cover next!
leetcode.com/p...
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Let's connect on LinkedIn!
/ aleksmitrovic
Follow me on Medium!
/ mitrovic.aleksandar
Questions or suggestions for new videos? Email me!
babybear4812@gmail.com

Пікірлер: 61
@maxgera4870
@maxgera4870 3 жыл бұрын
Thanks for the video. The time complexity for this problem is O( N log N), there is optimal solution with O(N log K) where K the number of employees.
@jamjam3448
@jamjam3448 2 жыл бұрын
i solved it in O(n) time by using the sliding window technique.
@vatssv3006
@vatssv3006 Жыл бұрын
I had been looking for good solutions/explanations for this one. This is beautiful really. You somehow made it look pretty easy tbh.
@babybear-hq9yd
@babybear-hq9yd Жыл бұрын
thanks bro glad you found what u were looking for :)
@AlexBlack-xz8hp
@AlexBlack-xz8hp 5 ай бұрын
Your video took this from being pretty incomprehensible to me to being super trivial in 20 minuts. Thank you sooooo much for your very clear explanation. It was incredibly helpful.
@babybear-hq9yd
@babybear-hq9yd 5 ай бұрын
glad it helped bro!
@saitmesutcanilhaner8850
@saitmesutcanilhaner8850 5 ай бұрын
Best explanation I have seen. Thank you very much! The solution seems simple but it is not easy to come up with the solution on the spot especially given the limited amount of time in the interviews. I don't think I would be able to fully solve this myself in 20 minutes.
@babybear-hq9yd
@babybear-hq9yd 5 ай бұрын
simple != easy :)
@CDJohnson
@CDJohnson 3 жыл бұрын
Great explanation! Your solution is a lot more intuitive than the solutions on LC. Subscribed!
@babybear-hq9yd
@babybear-hq9yd 3 жыл бұрын
Thanks Christian!! Super happy that it helped :)
@jamjam3448
@jamjam3448 2 жыл бұрын
Thanks bro. All i wanted was to understand the question and then solve it by myself which i did in O(n) time. You explained the question well.
@babybear-hq9yd
@babybear-hq9yd 2 жыл бұрын
appreciate it buddy, happy it helped!!
@ShivangiSingh-wc3gk
@ShivangiSingh-wc3gk 2 жыл бұрын
Thank you for an awesome walkthrough and explaining the concept with pictures.
@babybear-hq9yd
@babybear-hq9yd 2 жыл бұрын
My pleasure!
@alifyamohamedali1002
@alifyamohamedali1002 3 жыл бұрын
Thank you so much for your explanation :). Wow you made it seem so trivial in the end. Blows my mind
@babybear-hq9yd
@babybear-hq9yd 3 жыл бұрын
LFG!!
@M0ren025
@M0ren025 3 жыл бұрын
this was really well explained.... Thank you so much, I spent hours looking at this and your video finally helped me understand (:
@VijayKumar-my7ye
@VijayKumar-my7ye 2 жыл бұрын
I think the key to this problem might be merging the sorted lists to get a final sorted list and then finding the free slots in that final list. The problem is tagged under Merging Intervals so maybe thats being tested here. Its also possible to find the free slots as we get a stream of sorted Intervals while doing the merging. No need to loop again on final list.
@coding8000
@coding8000 2 жыл бұрын
God level Explanation, best on youtube!. Thanks man., love from Seattle.
@babybear-hq9yd
@babybear-hq9yd 2 жыл бұрын
thanks buddy! hope you're holding the fort down over there
@coding8000
@coding8000 2 жыл бұрын
Thanks., Watched all of ur videos, please keep uploading more videos, more frequently., thanks.
@chair_smesh
@chair_smesh 2 жыл бұрын
If you had a cooler thumbnail like Neetcode and covered all the questions from Grokking the Coding Interview Patterns, I guarantee your channel will blow up
@MohamedMagdy-00
@MohamedMagdy-00 2 жыл бұрын
Great explanation ! Thanks
@rahulbawa3969
@rahulbawa3969 3 жыл бұрын
That was a really nice and easy explanation 👍
@sunginjung3854
@sunginjung3854 3 жыл бұрын
this is a great explanation
@chair_smesh
@chair_smesh 2 жыл бұрын
Thanks, Wolverine!
@babybear-hq9yd
@babybear-hq9yd 2 жыл бұрын
HAHAHAH
@shanjiang2078
@shanjiang2078 3 жыл бұрын
Great explanation! thank you so much!!
@babybear-hq9yd
@babybear-hq9yd 3 жыл бұрын
Thanks Shan!! My pleasure :)
@juanhernandez-up4pg
@juanhernandez-up4pg 3 жыл бұрын
Great teacher ☺️
@babybear-hq9yd
@babybear-hq9yd 3 жыл бұрын
thx juan :)
@harleenkaur4599
@harleenkaur4599 2 жыл бұрын
Thank you for the video. Quick question - what if instead of busy time, free times are given for all the employees. How can I get optimal solution in that case?
@TULKICK
@TULKICK 2 жыл бұрын
Do everything same till merge and sort. Then if there is an overlap, store [max of start times, min of end times].
@pranabsarkar
@pranabsarkar 2 жыл бұрын
Thanks a lot Guru !
@babybear-hq9yd
@babybear-hq9yd 2 жыл бұрын
pleasure's all mine :D
@deionshall5446
@deionshall5446 3 жыл бұрын
Awesome solution! I am a little confused about why we used lambda here? I was able to pass using the following code
@deionshall5446
@deionshall5446 3 жыл бұрын
//sort intervals intervals.sort() //find free time result = [] prevEnd = intervals[0][1] for start, end in intervals: if start > prevEnd: result.append(Interval(prevEnd, start)) prevEnd = max(prevEnd, end) print(result) return result
@babybear-hq9yd
@babybear-hq9yd 3 жыл бұрын
@@deionshall5446 Hey Deion, I believe that by default when sorting a "list that is nested with multiple other lists" the built in sort function in Python (i.e. Timsort) will sort it by the first element of the nested lists. The reason I broke it out separately here was just because I think it may have made the logic a bit easier to follow (i.e. by strictly addressing the fact that we are sorting by start time). In some questions, we may also want to sort by end time, or other parameters in this list, in which case a lambda function would be necessary (you could see examples of this in the Vertical Traversal of a Binary Tree video, for instance). So, strictly speaking, you're right. We didn't necessary have to use a lambda function in this particular scenario :) Does this answer the question?
@deionshall5446
@deionshall5446 3 жыл бұрын
@@babybear-hq9yd awesome thanks for breaking that down. I see what you mean now. I never used lambda functions like that , but I realize how useful they can be in algos! Thanks for taking the time to explain that
@sensen338
@sensen338 3 жыл бұрын
Very well explained :)
@davidlee588
@davidlee588 2 жыл бұрын
My JS solution is much intuitive, I wonder if this will pass the time complexity if I use flat(): const freetime = function (hours) { let copy = hours.flat().sort((a, b) => a[1] - b[1]); let result = []; let prev = copy[0]; for (let i = 1; i < copy.length; i++) { if (copy[i][0] - prev[1] > 0) result.push([prev[1], copy[i][0]]); prev = copy[i]; } return result; }; Thank you for your comment.
@babybear-hq9yd
@babybear-hq9yd 2 жыл бұрын
no idea what the time complexity of the `flat` function is buddy sorry. It's probably good to mention in an interview that that's an option, though a follow-up question would be what the complexity is, and perhaps how to implement it manually if you needed to. Can't say for sure but just some thoughts that come to mind first
@srinivasnisha03
@srinivasnisha03 3 жыл бұрын
What would be the run time of the algorithm? I am thinking Time complexity would be O(NlogN) (N is it he number of intervals) and pace complexity is O(1) or maybe O(N) since we are creating the intervals list.
@babybear-hq9yd
@babybear-hq9yd 3 жыл бұрын
You're correct! The time complexity would be O(NlogN) because of the sort on line 22, and space would be O(N) because of the interval's list :) I'll add this to the description of the problem as well for future reference.
@srinivasnisha03
@srinivasnisha03 3 жыл бұрын
Thank you for replying !
@Vlad1998996
@Vlad1998996 Ай бұрын
you are great
@goldfish8196
@goldfish8196 2 жыл бұрын
Wow! So great. Thank you!
@babybear-hq9yd
@babybear-hq9yd 2 жыл бұрын
welcome :D
@tylercondon3453
@tylercondon3453 2 жыл бұрын
Has an interviewer ever asked you to solve this problem or any interval problem without first sorting the intervals?
@babybear-hq9yd
@babybear-hq9yd 2 жыл бұрын
Nope! I’ve lucked out :’)
@gurupreetsingh8347
@gurupreetsingh8347 2 жыл бұрын
thanks good explanation but what about the Time complexity of nested LOOPS ? I guess its no of employees * no of intervals in another words - Each employee * intervals of each employee , am i right ? So I think for nested looping Time complexity would be O(K*N) only (here K is not constant actually, Can't be constant infact!! So we can ignore it by just treating like constant value) and for sorting Time complexity is O(N LOG N) and O(N LOG N ) is lesser than O(K*N) am I right ? So over all Time complexity would be O(K*N) right not O(N LOG N) ? Please correct me if I am wrong!! :)
@TULKICK
@TULKICK 2 жыл бұрын
If N is the number of intervals and K is the number of employees, then on avg each employee will have N/K intervals each. So, for each employee, the inner loop will run N/K times. Thus, overall the entire nested loop will run K*(N/K) times, = N times. So, complexity will be O(N), for nested loop. Overall complexity is O(NlogN) only.
@harvitech8871
@harvitech8871 3 жыл бұрын
Nice explanation
@babybear-hq9yd
@babybear-hq9yd 3 жыл бұрын
Happy to help! :)
@goodpeople7615
@goodpeople7615 3 жыл бұрын
Baby bear great 🐨🔥 But is it making use of the fact that times are given in sorted order !!
@babybear-hq9yd
@babybear-hq9yd 3 жыл бұрын
Thank you! It does not, because notice that it's the individual employee times that are in sorted order, not an overall collection of all possible times. In fact, that piece of information is a bit of a red herring. In lines 16-19 we group all the individual times into one big batch of times, which then needs to be sorted. Does that make sense? :)
@goodpeople7615
@goodpeople7615 3 жыл бұрын
@@babybear-hq9yd I want to say that maybe sorted bcoz of some reason but ur approach seems easier to understand :)
@adamsoliev6667
@adamsoliev6667 3 жыл бұрын
Thanks
@RohanPatel-xh2lc
@RohanPatel-xh2lc 3 жыл бұрын
Subscribed.
@babybear-hq9yd
@babybear-hq9yd 3 жыл бұрын
welcome :D
@Zoronoa01
@Zoronoa01 2 жыл бұрын
great explanation, thank you!
CANDY CRUSH (Leetcode) - Code & Whiteboard
27:43
babybear4812
Рет қаралды 15 М.
NUMBER OF SHIPS IN A RECTANGLE (Leetcode) - Code & Whiteboard
28:30
babybear4812
Рет қаралды 4,9 М.
From Small To Giant Pop Corn #katebrush #funny #shorts
00:17
Kate Brush
Рет қаралды 73 МЛН
РОДИТЕЛИ НА ШКОЛЬНОМ ПРАЗДНИКЕ
01:00
SIDELNIKOVVV
Рет қаралды 3,4 МЛН
iPhone or Chocolate??
00:16
Hungry FAM
Рет қаралды 53 МЛН
Employee Free Time: 759 - meta interview question
10:42
Destination FAANG
Рет қаралды 783
ALIEN DICTIONARY (Leetcode) - Code & Whiteboard
32:45
babybear4812
Рет қаралды 7 М.
How I Failed the Google Coding Interview (and lessons I learned)
14:24
RESTORE IP ADDRESSES (Leetcode) - Code & Whiteboard
23:40
babybear4812
Рет қаралды 3 М.
Whiteboard Coding Interviews: 6 Steps to Solve Any Problem
15:18
Fullstack Academy
Рет қаралды 372 М.
RANDOM PICK WITH BLACKLIST (Leetcode) - Code & Whiteboard
19:16
babybear4812
Рет қаралды 1,9 М.
Oh, wait, actually the best Wordle opener is not “crane”…
10:53
COUNT UNHAPPY FRIENDS (Leetcode) - Code & Whiteboard
14:41
babybear4812
Рет қаралды 3,6 М.
WHY IS THE STACK SO FAST?
13:46
Core Dumped
Рет қаралды 158 М.
From Small To Giant Pop Corn #katebrush #funny #shorts
00:17
Kate Brush
Рет қаралды 73 МЛН