• 1 week ago
Transcript
00:00I have created my class, I have imported the utility package and I have my main function.
00:06First of all, I will erase my end and start time.
00:10My start time is going to be equal to 130585.
00:16My end time will be equal to 246799.
00:22Now I have the case that I already have sorted activities on the end time basis.
00:30After that, we will write a whole code and see that if it is not sorted, then how do they
00:35sort it?
00:36But now, let's assume that it is sorted, because the question is already given on the end time basis.
00:41What I have to do? I will take my count, I mean maximum activities, I will write it, maximum activities.
00:48With this, I will take an array list, which I will store the index of the activities.
00:55New array list.
00:57Now, in the beginning, my maximum activity performed, I will add my first activity to add my first activity.
01:08I will add my first activity to my first activity.
01:10Now, my loop will be because every time I will perform my first activity,
01:15because my first activity will be the most quickly and the most quickly will be the most quickly.
01:19I mean, that after that, I will have maximum time for the other things to complete.
01:23So, I will run the loop for I equals 1, I less than when I start or my end.
01:29We can take anything here.
01:31Start or end.length.
01:32I plus plus.
01:34Every time I will analyze it.
01:36When I will do it, I will select activity.
01:37Then, when my first activity is the start time, the last chosen activity is the end time.
01:44So, I will take another variable here.
01:47Last end.
01:48Last end what will happen?
01:50In the beginning, I will select the first activity.
01:52So, last end will be the end time.
01:55This is the end of 0.
01:56This is my last ending time.
01:59If I want to do it, then what will I do?
02:01I will do it with the max act.
02:02I will do it with initialize 1.
02:03I will do it with the start of 1.
02:06So, since I will write 1 first time.
02:08I will write the code in class.
02:10For understanding, I will do it with initialize 0.
02:13Then, I will assign value 1.
02:15When I will add my first activity.
02:18So, I will check here.
02:19If my start of I is greater than or equal to my last end.
02:25In that case, I will select my activity.
02:29I will select my activity.
02:31It means that I will increase my maximum activities by one.
02:34And in my answer, I will add I.
02:37I will add the answer to the answer.
02:40And with that, I will update my last end.
02:43Now, my last activity is the ending time.
02:45That becomes end of I.
02:49So, this will be the proper condition.
02:51It will be the proper activity to select.
02:53And finally, it will be the answer to it.
02:56It will be the simple loop.
02:57It means, there is so much code.
02:59It will be the greedy approach.
03:02Now, I will just print it.
03:05Maximum activities is equal to my max act.
03:12I will print it.
03:14I will print it.
03:15I will select it.
03:16I less than answer.
03:18Size
03:18i plus plus.
03:23I will print it.
03:25I will print it.
03:25Get i.
03:26index.
03:29I will print it.
03:30Save.
03:31I will summarize.
03:32I have some activities given on the start and ending time.
03:36This is the sorted activities in the end time.
03:39What have I done?
03:40I have two variables.
03:41One is my maximum activities.
03:43One is my answer.
03:45I have taken a third and temporary variable.
03:48the last end which I store in which I store the last activity and what was the ending time.
03:53In the beginning, I chose the first activity and stored the ending time.
03:57Now I'm going to take a loop from one to end.
04:00I check every time that my current activity is the starting time greater than equal to last ending time.
04:06If this activity is non-overlapping and we can select this.
04:11Select the maximum activity increase in the answer.
04:15Add your i-th index, i-th activity and last end to the ending time of my i-th activity.
04:22In the last, I have printed my answer.
04:25Let's save and run.
04:28In the answer, I have come.
04:31Maximum activities 4 we can perform.
04:33Which activities are A0, A1, A3 and A4?
04:37This is the same answer that I got to dry run on my particular example.
04:41How much time is in it?
04:42If I look at the attention of time, I have done a simple loop every time.
04:47The whole process of the time complexity will be big of N.
04:52Now there is another special thing.
04:54This is the sorted activities on my ending time.
04:58Let's say that I have unsorted activities.
05:00I have just put the index on the activities.
05:02And I have to use the sorted activities.
05:04I have not done a lot.
05:05So, what can I do in it?
05:07I can make an array, a 2D array.
05:09I can do this and show you how the sorting process is done.
05:13How will we do?
05:14I will make an array by 2D array.
05:15I will put the name activities.
05:18In this case, I will put it in size and size.
05:22Start.length and 3.
05:26So this array is something like this.
05:29My 2d array is something like this.
05:31There will be three columns inside.
05:34This is my first column,
05:37this is my second column and third column.
05:39I can say this is my 0th column, this is my first, this is my second.
05:43And many rows inside this.
05:46First row is my activity 0,
05:49this is activity 1, this is activity 2,
05:51this is activity 3, this is activity 4 and this is activity 5.
05:54Just like here and here.
05:56And in the 0th column you will store the index.
05:59I will store the index here.
06:02I will store the same activities in the jumbled order.
06:05If it is sorted in the start basis,
06:08then 1, then 3,
06:11then 5, then 1, then 5 and then 8.
06:15Then I will get sorted here.
06:18Then 2, then 4, then 7, then 9, then 9, then 9.
06:24So if I give this activity,
06:26this is the wrong order of the activities,
06:28then there will be no solution.
06:29So I will sort them on the ending time basis.
06:31What will I do?
06:32I will store them first.
06:34I will store them first.
06:35In activities,
06:36what is the index 0?
06:38I have to store them here,
06:40here I will store them start time,
06:40here I will store them end time.
06:43So start will be 0,
06:45end will be 6.
06:46What will I do?
06:47I will pick this activity.
06:49Now I will pick the second.
06:49I will pick the second.
06:51I will pick the second here.
06:52I will write index 1,
06:53its start time 1,
06:54its end time 2.
06:56Then I will pick this activity.
06:57I will write index 2,
06:59its start time 3,
07:01its end time 4.
07:02In this way,
07:03I will store the 0th column,
07:05then start store,
07:06then end store.
07:08In this way,
07:09I will write code.
07:09What do I do?
07:10I will create activities called 2D area,
07:12which size is n x 3,
07:15because there are 3 columns.
07:17Now I will write a loop,
07:18for int i equals 0,
07:21i less than start.length i++.
07:26What will I do?
07:28In activities of i 0,
07:31what do I do?
07:32I will store my index,
07:34i.
07:34I.
07:36Activities of i 1,
07:39I will store my index,
07:41start of i.
07:43And activities of i 2,
07:47I will store my index,
07:49end of i.
07:50So,
07:50this way,
07:513 information store.
07:52Now,
07:53your question is,
07:54why do i store it?
07:56Because i is a index,
07:57already,
07:58i.th index,
07:59no sense to it.
08:01I will say,
08:02I will say,
08:02no sense to it.
08:03No sense to it.
08:05But,
08:05once again,
08:06it will be sorted,
08:08then,
08:08the original index,
08:09the activity,
08:11so,
08:11I will store this original index.
08:13So,
08:15the activities,
08:16when I have sorted,
08:16the original index,
08:19the original index,
08:20already,
08:21stored,
08:21and I will sort,
08:23I will write a single line,
08:25arrays.sort,
08:26which I will pass,
08:28in which I will pass,
08:28my activities,
08:292D array.
08:30Now,
08:312D arrays,
08:32we have never seen sorting before,
08:34so,
08:342D arrays,
08:35we will sort,
08:35on this way,
08:37which I will sort,
08:37on the column basis,
08:38I will sort.
08:39I will write here,
08:41second parameter,
08:42I will pass,
08:42I will pass,
08:44comparator,
08:45.
08:46comparing double,
08:47comparing double,
08:48I will write,
08:49o,
08:50then,
08:50I will create arrow,
08:51arrow ,
08:52.
08:52.
08:52.
08:52.
08:53.
08:53.
08:54.
08:55.
08:55than sign. So, this is an arrow, and then I will write O of 2. This line, this line
09:05is called lambda function. Lambda function is basically short form of a big function.
09:15Big function is a comparator. Comparators, which I have briefly talked about a brief
09:25sort of logic. Comparators, you will find a specific reading material in the
09:31reading. Comparators are important. We can practice it. Whatever we need to use in class,
09:40I will explain it together. So, if we write any function in short form, we call it lambda
09:45function. This is also used in JavaScript. So, in Java, I have written a lambda function.
09:51And this is a comparator. This is a fixed syntax. I have always written
09:55in the second parameter, comparator.Comparing. Double. Then I will write O. And then what
10:00I will write. Arrow, O of 2. Here, basically, I am telling Java, sorting on which basis
10:06you have to do. Here, I have told you that sorting is column number 2 basis. We have to
10:11focus on, if we remember, what was stored in column number 2. If this is my 2d array,
10:17it is stored in column number 0, in column number 1, in column number 1, in column number 2,
10:22I have stored my ending time. So, what is going to happen? Java will automatically
10:26sort it. How will it sort it? Which one of the ending time is small, will take it up?
10:32Which one of the ending time is small will take it up. Which one of the ending time is big,
10:33that will take it down. It will not change if start time or index is small. What is
10:37I will copy the start time index with the start time index, but the ending time which is less than
10:41the end time is lower than the end time.
10:44So, with this lambda function, I have sorted a 2D array in single line.
10:50Which logic method I have sorted?
10:52Simple, I have defined a logic that the ending time is the column to sort on the basis of the
10:57column.
10:58If I have to do the basis of start time, I could write this one, but I need the ending
11:02time for a good logic.
11:03So, I have sorted my 2D array in this way.
11:06Now, I can change the whole code according to this 2D array.
11:10How will I change?
11:11I will add here.
11:13Now, if I consider the activities,
11:16according to my answer,
11:19I should add activities.00
11:26I will add the end time.
11:39I will add the end time to the end time.
11:51So first, I need start time for the next activity. Last time, I already know here, I will add activities
11:59of i0, because I will add index to the answer, and here I will add activities of i2, ending time.
12:09So this way, my whole code will change. How will it change? Basically, if I have sorted my start and
12:14ending time, I will give them here.
12:17I will give them all the time. I will give them all the time, because it is very important.
12:22I will use all the same logic in many questions. One time I will run it.
12:27So the same answer will be printed. Maximum activities is equal to 4. Which activities is also coming.
12:32I will give them all the time, and I will give them all the sorting.
12:36Because if I have not given the question, that it is sorted, then we will write this whole code.
12:41You will have to write yourself. And there is a lot of times that there is a question of activity
12:44selection, but it is not sorted in the end.
12:47That's why we should know that in Java, how do we sort in a 2D area?
12:51In fact, how do we sort objects? Using a comparator.
12:55That's why comparators, which is the reading material, that is very important.
12:59How do we know about sorting in other cases?
13:01We should know about objects. Specifically, activity selection.
13:04So we will solve the question in this way. If we will sort this.
13:09In sorting, there will be extra time complexity in logging.
13:12Then my question will change the time complexity.
13:16And log n.
13:18And give it our bit.
13:18And give it our bit.
13:19.