- 3 days ago
Transcript
00:00Next question, which we will talk about is its name is job sequencing problem.
00:04We will give many jobs and jobs which we will sequence.
00:08The question is that we will give an array of jobs
00:11in which every job has a deadline and a profit.
00:16We will give many jobs.
00:18Every job is before a deadline.
00:22The deadline starts with 1.
00:24This is job A which is 4.
00:27And if you do this work, you will get the profit of 20,000.
00:31You will get the profit of 20,000.
00:32Or you can think of 20,000.
00:35The job always needs to be finished before your deadline.
00:39This is a given condition.
00:40This is also given that every job takes a single unit of time.
00:45That means we will finish a unit of time.
00:49Job B will finish a unit of time.
00:53In this way, we will finish all jobs individually.
00:55We will finish a single unit of time.
00:58It means that we can think of this time
01:00that we will make one job 1 hour.
01:02This job A is before 4 hours.
01:05This job B should be before 1 hour.
01:07This job B should be before 1 hour.
01:09This job B should be before 1 hour.
01:09Job C also has a deadline for 1 hour.
01:12We can complete it until 1 hour.
01:13So, the minimum possible deadline is 1 hour.
01:17The minimum possible deadline is 1 hour.
01:19The minimum possible deadline is 1 hour.
01:21The minimum possible deadline is 1 hour.
01:22Maximize the total profit.
01:24Which we can get if we can schedule one job one time.
01:27We can schedule one time.
01:29We can schedule one time.
01:31Every job we can schedule one time.
01:33Every job is our deadline.
01:35Our profit is our deadline.
01:36Our profit is our deadline.
01:36We can do which jobs to make maximum profit.
01:40We need to find out.
01:42If I talk about this example.
01:44I will talk about it.
01:45I have 4 jobs.
01:46The job A is which deadline is 4.
01:48The profit is 20.
01:50The job B is which deadline is 1.
01:52The profit is 10.
01:53The job C is which deadline is 1.
01:55The profit is 40.
01:56The job D is which deadline is 1.
01:58The profit is 30.
02:00For this, the answer will be that
02:02I will do job C and job A.
02:04Then I will have maximum profit.
02:06We can think of different variations.
02:09Let's take the first sequence.
02:10Sequence or order.
02:11First you have done which job?
02:13Later you have done which job?
02:14Let's take the first job A.
02:16If I have done a job A,
02:18then if I have done a job A,
02:19then I can do any job after that.
02:21No job A can do any.
02:21No job A can do any.
02:22Why?
02:22Because I have much time to do job A.
02:24I have much time to do that.
02:26Time that I have used to do that.
02:27That will be equal to 1.
02:28Because one job A can do one time.
02:30Now the time A has 1.
02:32So I cannot do any other job.
02:34Because the rest of my work is the deadline.
02:37My deadline was equal to 1.
02:39Now I cannot do one after that.
02:40I cannot do any job A.
02:42So I should not do job A.
02:43I should not do first.
02:44Because the time will be one.
02:45How much profit will be?
02:47I have done a job.
02:49I have done a job.
02:51Equal.
02:51Another possible sequence.
02:53This can be possible.
02:54I will do job B.
02:56Before I have done a job B.
02:58Then what will I do?
02:59How much time will be?
03:00Time will be equal.
03:01How much profit has come now?
03:03I have come to 10.
03:05Because I will do 10,000.
03:08I will do 10,000.
03:08I will do 10,000.
03:08I will do 10,000.
03:10I will do 10,000.
03:10Now I have time.
03:11Now I cannot do C.
03:14Because it was deadline 1.
03:15Time 1 has been set.
03:16D.
03:17I cannot do deadline 1.
03:18Because it is deadline 1.
03:19Time 1 has been set.
03:20But I will do 1.
03:20Because I will do deadline 4.
03:23I will do what to do then.
03:24After B, I will do A.
03:25Time is now.
03:26I will do 2.
03:27How much profit has been done?
03:2810 plus 20 which is equal to 30
03:31in sequence ko
03:32my here
03:343rd sequence
03:35if I see
03:353rd sequence
03:37I have done
03:38job C
03:38I have done
03:39C I have done
03:41C I have done
03:41C I have done
03:42time I have done
03:441
03:44profit is equal to 40
03:46now C
03:47time if I have done
03:48already done
03:49D I will not do
03:50because it's deadline
03:52B I will not do
03:54because it's deadline
03:55I can do
03:55because it's deadline
03:574
03:57so I will do
03:58C I will do
04:00time for me
04:012
04:01in 40
04:02in 20
04:03and add
04:03final profit
04:05in this
04:06other sequence
04:07the other sequence
04:09can be done
04:10D
04:12if I have done
04:13D
04:13how much time for me
04:15time for me
04:161
04:16profit
04:1830
04:19if I have done
04:21time for me
04:221
04:22then I can't do
04:23C
04:23B
04:52D
04:54total profit
04:55I have
04:55maximum profit
04:57total profit
04:57and I will find out
04:59this total profit
04:59and total sequence
05:01I have to find out
05:02using greedy algorithm
05:04now
05:04to solve this question
05:06the most basic
05:08approach
05:08is the brute force
05:10approach
05:10we will do
05:33which kind of
05:35have from other
05:38prefered
05:40things
05:41what we want to do with our jobs. If we think about greedy mindset,
05:45a little bit with a lalaj, then we want to do which work we want to do
05:48first. We want to do that work which will be more profit.
05:52Because here I have to do the maximum profit. The first question was
05:56activity selection. What was I have to do? I have to do maximum work.
06:00I had to do maximum activities. So there I have to do more work.
06:04Here I have to do the maximum profit. So here I have to do the maximum profit.
06:08So here I have to do maximum profit. So first of all,
06:10that is the maximum profit profit. So in the greedy algorithm,
06:13whatever thing is minimum or whatever is maximum, I have to do
06:16sorting on their basis. If I want to go out maximum profit,
06:20I will do the basis of the profit. If I want to do maximum work,
06:24I will do the basis of the ending time. I will do maximum value,
06:27minimum weight. So to sort on the basis of the ratio.
06:30Like I had in the fractional knapsack. So here I have a little
06:33hint with a little practice that we have to do the same thing,
06:36which you can do maximum on the basis of sorting
06:39which you can do minimum on the basis of sorting
06:42so here I have to do profit on maximum
06:44so I will do my job on the basis of my job
06:49so my first job is to sort
06:55which basis of
06:57based on profit
07:00I will do my job on the basis of my job
07:03so I will sort it
07:03so before we have to do that
07:05first of all, my job is C
07:07which is time, deadline is 1
07:09profit is 40
07:11that is my job D
07:14which is deadline is 1
07:16profit is 30
07:16then my job is A
07:19which is deadline is 4
07:21profit is 20
07:21then my job is B
07:24which is deadline is 1
07:26profit is 10
07:27now the second step is my time
07:30I will track my time
07:31what is the time?
07:32what is the time?
07:32now the time is 0
07:33which is the time
07:35which is the area list
07:36which is the sequence of jobs
07:38to add
07:39so what will I do
07:40I will do a loop
07:40for int
07:42i equals 0
07:45i
07:46i
07:46i
07:46i
07:46i
07:46i
07:47i
07:47i
07:48i
07:59i
08:00i
08:00i
08:00is there
08:00the current time
08:02has yet
08:02so that
08:03possible
08:03if it is
08:06done
08:06because
08:08there is
08:08profit
08:09so
08:09what
08:10will
08:10we
08:11add
08:12process
08:13started
08:13to
08:13try
08:13us job ko add कर देंगे और अपने time को हर बार कर देंगे plus plus add job in answer
08:21this is my answer
08:23ya my sequence तो मेरा process क्या रहेगा मैं अपने time को शुरू कर दूँगी counter को शुरू कर दूँगी
08:28zero के साथ हर बार अपने jobs को उठाते जाओंगी सबसे पहले जो job मेरे पास आएगा वो maximum profit
08:33वाला होगा
08:34उसके बाद उससे थोड़े से कम profit वाला उसके बाद उससे थोड़े से कम profit वाला हर बार देख लूँगी
08:38कि क्या job की deadline निकल चुकी है
08:41job की deadline अगर नहीं निकली तो हमेशा time की value will be greater than job की deadline
08:45तो इस case में job को तो मैं answer के अंदर add कर दूँगी और time को कर दूँगी plus
08:50plus कि
08:50job को करने में मुझे इतना time up खर्च हो चुका है जैसे अब अगर इस example के उपर मैं
08:55अपनी approach को apply करूं तो सबसे पहले मैं आऊंगी कहां पर
08:58सबसे पहले मैं job c के उपर आऊंगी
09:00c के लिए मैं लेखूंगी क्या उसके deadline जा चुकी है अभी time की value क्या है time की value
09:05अभी 0 के बराबर है
09:06यहाँ पर condition जो है वो उल्टी होने वाली है मतलब time की value जो है less than मेरे job
09:12की deadline होने चाहिए
09:13तो time is less than one तो उस case में मुझे क्या करना है job को तो मैं add कर
09:17दूं अपने answer के अंदर
09:18यानी c को मैंने add कर दिया answer में and उसके बाद time को कर दूं plus plus time हो
09:22जाएगा one की equal
09:23फिर मैं जाओंगी job d पर अब job d को क्या add किया जा सकता है job d की deadline
09:29है one यह time से बड़ी नहीं है
09:32तो इसका मतलब इसको करने का time अब निकल चुका है इसको अब आप नहीं कर सकते है उसके बाद
09:37आजाओंगी job a पर job a की deadline है 4 के बराबर this is greater than my time
09:42मतलब इसको किया जा सकता है इसको किया जा सकता है तो इसको कर लूंगी करने का मतलब मैं answer
09:47में add कर दिया a को time को increase कर दिया by one
10:02तो मैंने 2 के बराबर time लगाया क्या sequence में मैंने jobs को किया पहले मैंने c को किया फिर
10:07मैंने a को किया this is my final answer
10:10तो approach क्या रहेगी यह मेरी approach रहेगी जिसके लिए मैंने sudo code लिख दिया है अब इस approach के
10:15लिए हम finally code को करने वाले है और इस code के अंदर एक और special चीज़ इस code के
10:19अंदर हम सीख रहे होंगे कि objects को कैसे sort करते है अब numbers को तो sort करना मुझे आता
10:27है
10:27objects को sort करने के लिए हमारे पास special हमें line लिखनी होती है collections.sort के अंदर जिसको हम
10:34इस वाले code के अंदर सीख रहे होंगे
10:35code के अंदर मुझे क्या दिया है मुझे मेरी classroom class है उसके अंदर main function है main function के
10:40अंदर मेरे पास 2D array है jobs information नाम का
10:43इस 2D array के अंदर हर एक job के लिए सबसे पहले उसकी deadline है उसके बाद मेरे पास उसका
10:49profit है मैं क्या करूंगी सारी की सारी information store कराने के लिए मुझे इस information को sort करना है
10:55sort करने से पहले मैं इस information को एक class की object की form में define करना चाहती हूँ
11:00तो मैं एक class बना लूँगी static class जिसको मैं नाम दूँगी job
11:05इस job से मैं blueprint बनाऊँगी कि एक job के अंदर क्या क्या होना चाहिए
11:09पहले तो उसके अंदर कुछ उसकी deadline होनी चाहिए
11:12उसके अलावा उस job का अपना कोई profit होगा
11:16इसके अलावा उस job की अपनी कोई ID होगी
11:19ID यहाँ पर मैंने ABCD नहीं लगी है
11:21यहाँ पर ID मैं 0, 1, 2, 3 लूँगी
11:250 का मतलब है मेरे पास A
11:271 का मतलब है मेरी ID B हो जाएगी job की
11:302 का मतलब है C हो जाएगी तो इस तरीके से
11:32IDs मुझे लेनी है हर एक job के
11:34इकोल तो मैं अब अपनी jobs
11:36information को इस job class की
11:38object की form में define करूँगी कैसे कर सकते
11:40मैं एक array बनाऊंगी
11:42जिसका type है job जिसके अंदर में
11:44बहुत सारे jobs को add करने वाली हो
11:46इस array की length हो जाएगी
11:47new int of jobs
11:50info dot length
11:55new job
11:56तो जितने मेरे पास jobs की
11:58information है उतनी ही
12:00size का मैंने objects का
12:02array बना लिया है किस type की objects है
12:04इस job type की objects है
12:06अब मैं इनके अंदर data store कर देती हूं
12:08for int i equals 0
12:10i less than
12:11jobs dot length i plus
12:14plus
12:18i is equal to new job
12:20जिसके अंदर
12:21मैं अपनी values को pass कर दूँगी
12:23तो इसके लिए मैं क्या करती हूं
12:24एक constructor बना लेती हूं
12:27public job
12:28इसके अंदर मेरे पास कोई id आ रही होगी
12:31कोई deadline आ रही होगी
12:32and कोई profit आ रहा होगा
12:35तो my
12:36id is equal to i
12:38my deadline is equal to d
12:41and my profit
12:42is equal to p
12:44इस तरीके से constructor मैंने बना लिया job के लिए
12:47तो पहले id पास कर दूँगी
12:48which is i
12:49फिर मैं deadline पास कर दूँगी
12:51which is jobs info
12:54of i
12:560
12:58फिर मैं profit पास कर दूँगी
12:59which is jobs info
13:01of i
13:021
13:030 त कॉलम पे मेरे पास deadline है
13:051st कॉलम पे मेरे पास profit है
13:07तो उसको मैंने अपने jobs i वाला
13:09iit object बनाने के लिए send कर दिया
13:11in fact इसको sort करने के लिए
13:13मैं arrays की जगे इसको array list type का बनाऊंगी
13:16ताकि मैं अपने collections को use कर पाऊं
13:18integer
13:19की जगे यहां पर यह array list बन जाएगी
13:22job की इसका नाम है jobs
13:24is equal to
13:26new array list
13:27अब मुझे size भी नहीं define करना पड़ेगा
13:30इसको मैं simply कर सकती हूँ
13:31jobs info dot length
13:35मैंने क्या किया
13:36अपनी array list बनाई jobs की जिसमें
13:38मैंने हर बार jobs के अंदर
13:39एक नई job type की object को add कर दिया
13:42अब मैं इस jobs की array list को
13:44sort करने वाली हूँ
13:44तो मैं लिखूंगी collections dot sort
13:47इसके अंदर pass कर दूँँगी jobs
13:49and इसके साथ साथ मैं define करने वाली हूँ
13:51अपना comparator
13:53comparator is a function जिससे logic
13:55define होता है comparison का
13:56कि sorting किस तरीके से होगी
13:58तो sorting अगर एक object
14:00A और B में हो रही है
14:01तो sorting के लिए यहां मैं lambda function लिख रही हूँ
14:04वही same lambda function जो मैं arrays dot
14:06sort के अंदर लिखते आए हैं उसको बस
14:08collections dot sort के अंदर लिखा जा रहा है
14:10अगर मुझे दो object A और B को compare करना है
14:13तो उसके अंदर comparison होना चाहिए
14:16based on their profit
14:18तो कैसे comparison हो
14:20A dot profit
14:22minus B dot profit
14:26तो मेरे comparison का parameter
14:28मेरा A और B का profit होना चाहिए
14:30यह किस चीज का syntax है
14:32इस syntax से मेरे पास
14:34ascending order of
14:36profit में सारी objects आ जाएंगी
14:39ascending order of profit मतलब
14:41जिसका profit कम है वो पहले आ जाएगा
14:43जिसका profit जादा है वो बाद में आ जाएगा
14:45पर मुझे तो किस में चाहिए मुझे तो जादा
14:47profit वाला पहले चाहिए तो मुझे अगर
14:49descending order में चाहिए तो उसके लिए मैं बस
14:50simplis को reverse कर दूँगी
14:52b.profit माइनस
14:53a.profit तो यह मेरा lambda
14:56function है जिसमें मैंने यह logic
14:57define किया कि a और b दो
14:59objects को अगर मैं compare कर रही हूं यहाँ पर
15:01a और b की जगह मैं लिख सकती हूं object 1
15:05और object
15:062 को compare किया जा रहा है
15:08तो parameter यह होना चाहिए कि object 2 का profit
15:13minus object
15:141 का profit
15:15इसका मतलब है कि descending order में
15:17मेरे पास sorting होनी चाहिए सारी की
15:19objects की अगर यहाँ पर मान लेते हैं
15:22कुछ students होते हैं जिनका roll number के basis
15:24पर sorting करनी होती हैं तो simply
15:26हम लिख सकते थे object 1
15:28dot roll number minus object 2 dot roll number
15:31तो इस तरीके से हम
15:32objects के अंदर sorting करते हैं कैसे
15:34sorting करते हैं यहां पर इस तरीके का
15:36एक custom comparator
15:38define करके यह जो है तरीका
15:40this is basically a lambda function
15:42जो short form है किसी भी comparator
15:44को लिखने की तो यहां मेंने क्या किया
15:46यह syntax हमें भी याद कर लेना
15:48यह syntax होता है कि नहीं भी objects को
15:50sort करने का in descending order
15:51ascending order के लिए क्या करना है
15:53बस इस object 1 को यहां लेना है
15:55और इस object 2 को यहां ले आना है
15:58अब एक बार जब मैंने अपने सारे
15:59jobs को sort कर लिया तो अब मेरा काम क्या है
16:01अपने sequence को store करना
16:02तो मैं यहां लिखोंगी character
16:04in fact character की जगे इस बार मैं ले लोंगी
16:06integer यह होगा मेरा sequence
16:09is equal to new array list
16:13अब मैं हर बार comparison शुरू कर दूँगे
16:15for int i equal 0
16:18i less than jobs.size i++
16:24अभी जो मेरा current job है
16:27that is equal to jobs.get i
16:32इस current job के लिए मैं compare कर लूँगी
16:34अपने time के साथ तो शुरूआत में time की value है 0
16:37अगर time से इसके deadline निकल चुके यह तब तो इसको
16:40profit को हम add नहीं कर सकते काम को भी नहीं कर सकते
16:43पर अगर deadline नहीं निकलिये तब हम इस काम को कर सकते हैं
16:45तो if my current job.deadline
16:48is greater than my time
16:50तो इस case में हम इस काम को कर सकते हैं
16:53काम को करने का मतलब है कि sequence के अंदर मैं add कर दूँ
16:56अपने current job की id को
17:00and time को मैं increase कर दूँ
17:02क्योंकि काम करेंगे तो time increase भी हो जाएगा उतना ही
17:04और last में क्या करूँ
17:06last में मुझे print करवा देना है
17:08अपने sequence को
17:10मेरा final answer क्या होगा
17:12एक तो मेरा final answer होगा कि
17:14maximum jobs
17:15जितने मैं कर सकती हूँ
17:17that will be equal to
17:18my sequence.size
17:22और वो कौन-कौन से sequence में होँगे
17:24i is equal to 0 से शुरू होँगे
17:26sequence.size
17:28i++
17:29हर बार print करवा दूँगे अपना sequence
17:36sequence.get i
17:43यहाँ पर next line
17:44इसको कर लेते हैं save
17:46एक बार मैं हलका सा summarize कर देती हूँ
17:48क्या किया मैंने
17:49अपने jobs का तो मैंने
17:50array list बना लिया
17:52सारी की सारी job objects को
17:53job की information को मैंने
17:55objects में convert किया
17:56और उसका मैंने array list बना लिया
17:57jobs की class मैंने इस तरीके से
17:59define किया है
18:00यह हो गया मेरा constructor
18:08करने के लिए यह syntax मैं follow करती हूँ
18:10जब भी मुझे descending order में sort करना होता है
18:12तो अब जिस भी parameter के basis पर sort किया
18:15वो यहाँ पर इस तरीके से dot profit
18:16अगर मान लेते हैं
18:17id के basis पर sort करना होता है
18:19तो यह dot id लिख देती
18:20अगर deadline के basis पर sort करना होता है
18:22तो यह dot deadline में लिख देती
18:38मैंने increase कर दिया
18:39last में maximum jobs को मैं print कर दूँगी
18:41और मैं अपने पूरी के पूरी sequence को भी
18:43output window के अदर print कर दूँगी
18:45इसको कर लेते हैं safe
18:48and now run
18:53मेरा जो answer आया है
18:54उसमें maximum दो jobs को perform कर सकती हूँ
18:56कौन से हैं दो jobs
18:58इसमें से सबसे पहला job है
18:59जो index 2 पर है यानि ये वाला
19:01which is basically C
19:02फिर दूसरा job है जो index 0 पर है
19:04यानि ये वाला job which is basically A
19:07profit कितना आएगा?
19:08it will be 20 plus 40 which is equal to 60
19:11तो इस तरीके से हम job sequencing की problem को solve करते हैं
19:14using greedy approach