WEBVTT

00:00:00.000 --> 00:00:05.920
I'm a senior AI engineer, and I've developed AI software factories that

00:00:05.920 --> 00:00:09.480
basically write all my code for me.

00:00:09.480 --> 00:00:13.680
Nevertheless, I still know how to code, and to prove it, I'm going to

00:00:13.680 --> 00:00:17.240
teach you how to approach a LeetCode problem.

00:00:17.240 --> 00:00:21.800
In this video, I'm going to talk about course schedule and how you can

00:00:21.980 --> 00:00:27.950
go about reading the problem and understanding the problem-solving skills to

00:00:28.020 --> 00:00:29.920
get the right answer.

00:00:29.920 --> 00:00:34.830
So if you want to get a tech job where you're making $200,000, $300,000,

00:00:34.900 --> 00:00:39.760
$400,000 per year, stop and save this video.

00:00:39.760 --> 00:00:43.560
Come back to it when you're ready to start studying LeetCode.

00:00:43.560 --> 00:00:47.260
Now, when you approach a problem like this, the first thing you want to

00:00:47.260 --> 00:00:50.720
know, the first thing that comes to top of your mind is this is

00:00:50.720 --> 00:00:55.530
a graph problem. And the reason why you know that instantly is because you

00:00:55.530 --> 00:01:00.020
use a spreadsheet like this that divides problems into different categories.

00:01:00.120 --> 00:01:03.860
So you have a category for dynamic programming.

00:01:03.860 --> 00:01:09.560
You have a category for graphs, a category for heaps, a category for arrays.

00:01:09.560 --> 00:01:14.720
You already know that this is a graph problem because you're using a spreadsheet

00:01:14.820 --> 00:01:20.650
like this or you've done enough practice problems to remember how to

00:01:20.650 --> 00:01:23.720
do it. So we know it's a graph problem.

00:01:23.720 --> 00:01:28.880
We're going to go here and we basically read the question and understand.

00:01:28.920 --> 00:01:34.820
It's basically asking us how to detect cycles in a graph.

00:01:34.820 --> 00:01:39.840
So to detect cycles in a graph, I'll first show you a graph.

00:01:39.840 --> 00:01:45.170
And with this picture, you can sort of see that with this example, there

00:01:45.170 --> 00:01:48.510
are no cycles. Let me type this out on the screen.

00:01:48.560 --> 00:01:54.270
So we have numCourses equals two, prerequisites equals one, zero.

00:01:54.320 --> 00:01:58.500
What this will look like if you draw the graph and we can see

00:01:58.500 --> 00:02:00.380
that we have no cycle.

00:02:00.380 --> 00:02:05.960
We have zero, which has no prerequisites.

00:02:05.960 --> 00:02:09.810
And we have one which has zero as a prerequisite.

00:02:09.840 --> 00:02:11.840
And we know this. How do we know this?

00:02:11.840 --> 00:02:13.840
How do we know how many courses we have?

00:02:13.840 --> 00:02:16.960
We are a total of numCourses.

00:02:16.960 --> 00:02:21.770
And so the high level approach to this problem is you're going to try

00:02:21.770 --> 00:02:25.040
to take every single course.

00:02:25.040 --> 00:02:29.940
If you take every single course and you have no cycle, there's no infinite

00:02:30.020 --> 00:02:34.440
recursion loop, then yes, you can finish the course load.

00:02:34.440 --> 00:02:38.970
But if while you're trying to take a course, you're in an infinite loop,

00:02:38.970 --> 00:02:43.330
you have a little cycle, then you're going to say, no, we can't finish

00:02:43.330 --> 00:02:47.700
the course. So this example here, pretty simple to see that we have no

00:02:47.700 --> 00:02:51.020
cycles. Course zero has no prerequisites.

00:02:51.020 --> 00:02:54.870
Course one only has course zero, which has no prerequisites.

00:02:54.960 --> 00:02:57.940
So we're going to return true.

00:02:57.940 --> 00:03:02.730
Now let's take a look at the second example here, where we have numCourses

00:03:02.730 --> 00:03:06.200
equals two and prerequisites equals this graph here.

00:03:06.200 --> 00:03:09.420
Let me draw it out on the screen right now.

00:03:09.420 --> 00:03:13.870
We have zero, and then prerequisite is one, and we have one, and the

00:03:13.870 --> 00:03:15.520
prerequisite is zero.

00:03:15.520 --> 00:03:20.040
And right off the bat, we see there's a cycle.

00:03:20.040 --> 00:03:23.600
We try to take course zero, but we first had to take course one.

00:03:23.600 --> 00:03:26.760
We try to take course one, we first had to take zero.

00:03:26.760 --> 00:03:28.760
This is a cycle.

00:03:28.760 --> 00:03:32.000
So we're going to return false.

00:03:32.000 --> 00:03:36.160
Now how do you write this up in code?

00:03:36.160 --> 00:03:41.760
Basically while you're on a current course, what you'll do is you're going to

00:03:41.760 --> 00:03:44.080
look at every single prerequisite.

00:03:44.080 --> 00:03:49.250
So let me actually draw this out right now.

00:03:49.280 --> 00:03:53.130
First let's just gather a list of all of our prerequisites.

00:03:53.240 --> 00:03:58.780
I'm going to erase all this on the screen, and I'm going to say

00:03:58.780 --> 00:04:05.330
for prereq and prerequisites, we're going to have a mapping,

00:04:05.330 --> 00:04:08.840
so it's called prereqMap.

00:04:08.840 --> 00:04:14.680
We're going to have a mapping of the course we're on to the prerequisites.

00:04:14.680 --> 00:04:17.920
And so basically the way we'll do this is we're going to say prereq

00:04:17.920 --> 00:04:24.560
zero. If prereq zero is in prereq

00:04:24.560 --> 00:04:30.380
map, then we're going to

00:04:30.490 --> 00:04:37.540
say prereqMap of prereq zero,

00:04:37.590 --> 00:04:38.000
dots

00:04:39.210 --> 00:04:45.870
append prereq zero or prereq one.

00:04:45.920 --> 00:04:50.360
We're basically going to append the actual prerequisite, and so otherwise if it's not

00:04:50.360 --> 00:04:54.710
already in the map, we're basically going to do the same thing, but we're

00:04:54.710 --> 00:05:01.370
going to say this is going to equal to the prerequisite.

00:05:01.400 --> 00:05:06.220
And then once we're done, we're just going to print the prereqMap, make sure we did it all

00:05:06.970 --> 00:05:10.000
correctly. You're allowed to run code during most interviews.

00:05:10.000 --> 00:05:13.140
If you can't run the code, you can at least draw it out, but

00:05:13.140 --> 00:05:16.800
you can run the code, make sure everything looks right.

00:05:16.800 --> 00:05:20.180
This looks right to me. This looks exactly like what I drew on the

00:05:20.180 --> 00:05:24.940
screen earlier. So the next thing I'm going to do is I'm going to

00:05:24.940 --> 00:05:27.280
remember several things.

00:05:27.280 --> 00:05:32.440
So number one, we're trying to detect cycles, and the strategy for detecting cycles

00:05:32.540 --> 00:05:36.360
is if we're visiting a node and we're visiting again, that is a cycle.

00:05:36.360 --> 00:05:40.090
So we're going to have a map of visiting or a set of things

00:05:40.090 --> 00:05:43.000
that we're visiting right now.

00:05:43.000 --> 00:05:49.310
And then we're also going to have another set that says can complete course.

00:05:49.320 --> 00:05:52.690
Basically we're going to save it in a little set that says we've already

00:05:52.690 --> 00:05:56.370
visited before. We determined that we can't complete it, so we don't have to

00:05:56.470 --> 00:05:58.760
go downstream and visit all these courses again.

00:05:58.760 --> 00:06:04.040
Like, there's basically a set of things that we know that we can complete.

00:06:04.040 --> 00:06:06.800
And so after we have that, what we're going to do, we know we're

00:06:06.990 --> 00:06:09.080
going to do depth-first search.

00:06:09.080 --> 00:06:12.720
So you can use breadth-first search as well, depth-first search is a little bit

00:06:12.720 --> 00:06:14.320
easier, it's just recursion.

00:06:14.320 --> 00:06:18.430
I'm going to type out the formula here, say pass, and we're going to

00:06:18.430 --> 00:06:20.630
implement that later.

00:06:20.640 --> 00:06:23.520
Right now what we're going to do is finish the rest of the function.

00:06:23.520 --> 00:06:29.460
So we're going to basically say for each course, for course and

00:06:29.550 --> 00:06:35.680
range of numCourses, we're going to basically say

00:06:35.830 --> 00:06:41.960
if I can't take the course, then I can't finish the course

00:06:41.960 --> 00:06:44.280
load, return false.

00:06:44.280 --> 00:06:49.160
But we can't return true here, but we basically have to first try to

00:06:49.160 --> 00:06:51.440
take every single course.

00:06:51.440 --> 00:06:56.210
We try to take every single course, and only after we've completed taking every

00:06:56.210 --> 00:07:00.000
single course can we return true.

00:07:00.000 --> 00:07:03.060
So we're not going to have any else statement, we're going to return true

00:07:03.060 --> 00:07:07.890
here. Again, let's just run the code, make sure things compile, and then we're

00:07:07.890 --> 00:07:12.660
going to get into the meat of the problem, getting this answer to be

00:07:12.660 --> 00:07:16.740
false. So let's implement our depth-first search.

00:07:16.760 --> 00:07:22.800
Number one, if course is in visiting, we return false.

00:07:22.800 --> 00:07:29.470
If course is in, you can complete the course,

00:07:29.470 --> 00:07:32.840
then we return true.

00:07:32.840 --> 00:07:38.250
And then at the very end, we want to return true, right?

00:07:38.320 --> 00:07:41.790
That's like the, we're going to turn on Boolean here, and let's actually implement

00:07:41.810 --> 00:07:43.360
the meat of the algorithm.

00:07:43.360 --> 00:07:49.270
Let's say we're going to get our prereqs, so prereqs equals

00:07:49.270 --> 00:07:54.540
prereqMap, and we could say prereqMap, of course, but what if the

00:07:54.830 --> 00:07:56.440
course is in the prereqMap?

00:07:56.520 --> 00:07:59.000
I don't remember if I implemented any logic here.

00:07:59.000 --> 00:08:04.840
I'm going to say prereqs equals prereqMap .get course, and

00:08:04.840 --> 00:08:08.400
then defaults to the empty array.

00:08:08.400 --> 00:08:14.350
And I'm going to say if length of prereqs equals zero, then I'm

00:08:14.430 --> 00:08:18.520
going to say we have no prereqs, so obviously we can return true.

00:08:18.520 --> 00:08:21.190
We have no prereqs for this course, so we can take the course.

00:08:21.320 --> 00:08:23.760
Obviously, we want to return true.

00:08:23.760 --> 00:08:27.230
And now we're going to, if we have prereqs, though, we need to see,

00:08:27.280 --> 00:08:31.500
can we take all of these prereqs, or do we get a cycle?

00:08:31.520 --> 00:08:34.180
So we need to remember to do several things here.

00:08:34.300 --> 00:08:36.970
So number one, we have to remember the visiting set.

00:08:37.080 --> 00:08:41.440
So let's say visiting set is a visiting.add the course.

00:08:41.440 --> 00:08:46.440
We do remember to remove it from the visiting set before we return.

00:08:46.440 --> 00:08:51.020
And finally, return true, again, we're basically creating a little cache here with can

00:08:51.180 --> 00:08:54.360
complete course, we want to add the course.

00:08:54.360 --> 00:08:58.590
If these steps are confusing, I can make a video going a little bit

00:08:58.590 --> 00:09:02.760
slower, but I'm trying to make it kind of fast.

00:09:02.760 --> 00:09:06.380
If we can complete the course, you know, we return true, then we then

00:09:06.380 --> 00:09:08.360
later on, we can kind of short circuit.

00:09:08.360 --> 00:09:10.040
That's sort of the idea behind this.

00:09:10.040 --> 00:09:12.720
So finally, we're going to iterate over all our prereqs.

00:09:12.960 --> 00:09:18.900
And prereqs, we're going to say if

00:09:18.900 --> 00:09:25.400
DFS, a prereq, if really if we can't take the course, same idea before,

00:09:25.400 --> 00:09:29.640
if we can't take the course, we return false.

00:09:29.640 --> 00:09:32.690
If we can take the course, we're not returning early, so we're not going

00:09:32.690 --> 00:09:36.260
to do anything here, we can't take the course, we return false.

00:09:36.280 --> 00:09:40.400
Otherwise, we don't do anything and this might be it.

00:09:40.400 --> 00:09:45.310
Let's run the code and see what happens.

00:09:45.320 --> 00:09:47.550
It looks like it runs for now.

00:09:47.560 --> 00:09:50.230
Let's click submit.

00:09:50.440 --> 00:09:54.000
LeetCode's UI changed a little bit and we're done.

00:09:54.000 --> 00:09:56.080
We got the right solution.

00:09:56.080 --> 00:09:59.930
Obviously, a real interview is going to talk to you about time complexity, they're

00:10:00.070 --> 00:10:02.800
going to ask you about space complexity.

00:10:02.800 --> 00:10:08.880
But this video is really describing how I break down the problem.

00:10:08.880 --> 00:10:15.680
If you have any questions at all, leave a comment below and I'll help you out.
