Job Sequencing Problem | Greedy Algorithms

Поделиться
HTML-код
  • Опубликовано: 17 янв 2021
  • Check our Website:
    In case you are thinking to buy courses, please check below:
    Link to get 20% additional Discount at Coding Ninjas: bit.ly/3wE5aHx
    Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
    Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
    Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    ---------------------------------------------------------------------------------------------------------------------------------------------------- I have decided to make a free placement series comprising of video lectures(C++ and Java) on the entire SDE sheet.. ✅(bit.ly/takeUforward_SDE)
    ✅Entire Series: bit.ly/placementSeries
    ✅Problem link: practice.geeksforgeeks.org/pr...
    C++/Java Code: takeuforward.org/data-structu...
    ✅Unacademy has launched a subscription that will help you learn and interact with your favorite teacher and will also help you clear your doubts! Check it out here: unacademy.com/goal/competitiv... (use coupon code TAKEUFORWARD to get 10% off)
    Join Unacademy's Pinnacle 2021; A Comprehensive and Concise Track to Become an Expert: unacademy.com/batch/pinnacle-...
    Check out Unacademy’s Free Classes and tests here (For Desktops):
    www.codechef.com/cptutorials?...
    unacademy.com/test-series/pro...
    unacademy.com/a/tech-placemen...
    ✅This is where I teach: ----- Use coupon-code "TAKEUFORWARD" for getting 10% on my course at GFG: bit.ly/striver_cpCourse
    ✅Use coupon-code "TAKEUFORWARD" for getting 10% for all GFG courses: bit.ly/tuf_gfgCourse
    If you appreciate the channel's work, you can join the family: bit.ly/joinFamily
    Thumbnail Creator: / rikonakhuli
    ✅ Striver's Linkedin Profile: / rajarvp
    ✅ Instagram: / striver_79
    Connect with us: t.me/Competitive_Programming_tuf (Use Link in Mobile only, if not works search "takeUforward" in telegram)..
    #dsa #leetcode #placements

Комментарии • 159

  • @aanchalmittal9897
    @aanchalmittal9897 2 года назад +25

    You make difficult concepts look easy.... Crystal clear explanation as always ❤

  • @vaishnavikammara8047
    @vaishnavikammara8047 7 месяцев назад +2

    I felt so difficult to do this problem, but the way you have explained made it simple to think. 🙌

  • @fritz6600
    @fritz6600 Год назад +9

    It's weird, I was able to get the logic once you visualized everything, I should focus on visualizing my thought process much more or elaborately I guess, thanks a lot!

  • @codingwithsmallsteps2878
    @codingwithsmallsteps2878 3 года назад +17

    Nailed it striver. Thank you for the awesome explanation. The clever part is doing the job (which can be done till very late) on the last deadline unit (if the last deadline unit for the job is not occupied or just as before the last deadline unit which is not occupied) so that in the meantime you can complete the other jobs and maintaining this using a visited array.

  • @pratikjain3323
    @pratikjain3323 3 года назад +39

    Your explanation skills are insane!! Thanks a ton. :)

  • @tombrady7390
    @tombrady7390 3 года назад +1

    since we know that values will be integers can we use a stable non comparison based sorting algorithm and bring sorting complexity down to linear

  • @ranasauravsingh
    @ranasauravsingh 2 года назад +2

    UNDERSTOOD... !!!
    Thanks striver for the video... :)

  • @sagargupta7458
    @sagargupta7458 3 года назад +9

    Please continue the playlist..

  • @abhimanyu6534
    @abhimanyu6534 3 года назад +1

    Which approach does interviewer asks
    The one that you taught or the disjoint set approach

  • @chandanbera2692
    @chandanbera2692 2 года назад +5

    @TakeUForward As per the explanation, the result array should contain (result[j] = arr[i].id) in line 71 of java code

  • @_inspireverse___
    @_inspireverse___ Год назад +1

    Can we solve it using knapsack method if we sort by deadline in ascending order?

  • @stith_pragya
    @stith_pragya 2 месяца назад +1

    Understood.........Thank You So Much for this wonderful video.......🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @avikmallick2493
    @avikmallick2493 Год назад +1

    instead of disjoint set u can use normal set too to it in nlogn

  • @varunmanchanda3972
    @varunmanchanda3972 3 года назад +4

    Bhaiya, I was trying to solve it using DSU but I am not getting a correct answer. Can you please make a video on it if possible?

  • @devanshmesson2777
    @devanshmesson2777 3 года назад +3

    A Big Big THANKS for your contribution♥

  • @lsrinivasamurthy
    @lsrinivasamurthy 2 года назад

    Thanks bro for the nice and clear explanation.

  • @hassanmohammed904
    @hassanmohammed904 2 года назад +1

    Hey Striver, Can you upload a video on Roti-Prata Spoj problem please? Thanks in advance !!

  • @adityatiwari1022
    @adityatiwari1022 2 года назад

    can we just sort it based on deadline (primary) and then sort it based on profit(secondary) That will just require linear time after sorting

  • @rahulsarkar913
    @rahulsarkar913 3 года назад +8

    Great work dada!!
    I found a question recently asked in amazon..
    Given an integer array arr of size N and an integer S. Return the list of all pairs of elements such that for each sum of elements of each pair equals to S.
    Note:
    Each pair should be sorted i.e the first value should be less than or equals to the second value.
    Return the list of pairs sorted in non-decreasing order of their first value. In case if two pairs have the same first value, the pair with a smaller second value should come first.
    Input Format:
    The first line of input contains two space-separated integers N and S, denoting the size of the input array and the value of S.
    The second and last line of input contains N space-separated integers, denoting the elements of the input array: arr[i] where 0

    • @manideepaktelaprolu3073
      @manideepaktelaprolu3073 3 года назад

      Sort the array and apply 2 pointer technique to find the pairs.

    • @yatinarora1252
      @yatinarora1252 3 года назад +8

      It is similar to two sum problem

    • @rishabmudliar558
      @rishabmudliar558 2 года назад +2

      Instead of sorting method use a frequency array which will make the solution O(n) instead of nlogn with n space complexity

    • @pawankumarnandagiri8202
      @pawankumarnandagiri8202 2 года назад

      @@rishabmudliar558 the array elements can be negative so,so we cannot take frequency array

    • @VinayKumar-ze2ww
      @VinayKumar-ze2ww Год назад

      @@pawankumarnandagiri8202 unordered map is useful then
      Interviewer can ask for returning indices rather than values, and if we sort array, we won't find the answer

  • @NavneetKumar-om1kc
    @NavneetKumar-om1kc 2 года назад

    can anyone tell what is the base of log when we are discussing time complexity?

  • @ilikememes9052
    @ilikememes9052 2 года назад +10

    I can't even solve this easy question on my own. I have to watch the video for almost every single problem on SDE sheet 😔
    I want to be SDE in a big tech company but now losing hope. 😢

    • @shadaabiqbal4875
      @shadaabiqbal4875 6 месяцев назад

      This is a hard level question not easy

  • @_-6912
    @_-6912 3 года назад +1

    I understood the solution and intuition!

  • @jakeperalta8135
    @jakeperalta8135 3 года назад +10

    In c++ code shouldn't we store arr[i].index in slot[j] according to solution explained earlier?

    • @shubhamsood1406
      @shubhamsood1406 3 года назад +1

      We can do that also, we only have to change the array value from -1 to any other number, both ways of changing the array value is correct.

    • @manavshah7450
      @manavshah7450 3 года назад +2

      Yes. That would be better if they ask u to print the job sequence.

  • @somyapratapsingh9849
    @somyapratapsingh9849 3 года назад +1

    The way of solving question 💯

  • @rudraprasaddash3809
    @rudraprasaddash3809 2 года назад +1

    Sir, how are you editing GFG default code? For me, the main function is already there and I just have to write the function only, but I want to write the code from scratch.

    • @ScienceSeekho
      @ScienceSeekho Год назад +5

      Then write in your local code editor na bhai

  • @chandan1929
    @chandan1929 Год назад

    for correct job sequence put slot[j]=arr[i].id; on line 47 c++ code

  • @diwakarkirar5102
    @diwakarkirar5102 Месяц назад

    After solving this question, I came here to look for the best solution and guess what it is what exactly I did, thank you so much for your videos that I am getting better everyday.

  • @asifd102
    @asifd102 10 дней назад

    thank you so much very good understanding

  • @jinhuang7258
    @jinhuang7258 Год назад

    Understood. Thank you!

  • @bhaveshkumar6842
    @bhaveshkumar6842 2 года назад

    Thank you so much!!!!!!!!!!!!

  • @adityamahimkar6138
    @adityamahimkar6138 2 года назад

    Bhai agar profits same ho toh phir id choose karneka kya? Just like we did in N meetings question or deadline like jiski deadline pehle voh pehle aise kuch ??

  • @gunashekarchenna3171
    @gunashekarchenna3171 3 года назад +3

    Striver, you are the saviour!!!!

  • @yashpaste7700
    @yashpaste7700 3 года назад

    Thank you, striver!

  • @devilcoder6432
    @devilcoder6432 2 года назад +1

    @take U forward can you please explain nlogn solution for this problem

  • @lord_yzal
    @lord_yzal Год назад

    easy and concise explanation

  • @devanshupadhyay2658
    @devanshupadhyay2658 Год назад

    best video ,related to greedy on the internet.

  • @mdshokeenkhan242
    @mdshokeenkhan242 Год назад

    in the question's example, we are not performing the job on the deadline which is not given, and in the video answers to deadline 3 are not given but we are performing the job on day 3 ...this is something that question changes???

  • @SatyaMantha
    @SatyaMantha Год назад

    Striver, you are awesome. Thanks a ton.

  • @tusharsharma6712
    @tusharsharma6712 2 года назад +30

    I was expecting priority queue solution from you for O(NlogN) time complexity

    • @neerajchoudhary3709
      @neerajchoudhary3709 2 года назад +5

      usko samjh hi nahi aya hoga .lol

    • @devrajgoswami4357
      @devrajgoswami4357 Год назад +20

      @@neerajchoudhary3709 you probably don't know who he's

    • @supriyamanna715
      @supriyamanna715 Год назад +2

      @@devrajgoswami4357 He's what he is! Striver_79

    • @simitoor1
      @simitoor1 Год назад +1

      did u get the solution in this time complexity

    • @supriyamanna715
      @supriyamanna715 Год назад

      @@simitoor1 I think MinHeap would do that

  • @binodbinod7537
    @binodbinod7537 3 года назад +1

    Thank you bhaiya!!❤️✨

  • @pranjalpimpale8465
    @pranjalpimpale8465 3 года назад

    where can I get the code?

  • @takeUforward
    @takeUforward  3 года назад +9

    C++ Code Link: github.com/striver79/SDESheet/blob/main/jobSequencingCpp
    Java Code Link: github.com/striver79/SDESheet/blob/main/jobSequencingJava
    Live sessions on Insta: striver_79

  • @aj9706
    @aj9706 2 года назад +1

    Brilliant wat an explanation

  • @lavanyam3224
    @lavanyam3224 2 года назад +2

    GOD LEVEL EXPLANATION!!!!

  • @mdshokeenkhan242
    @mdshokeenkhan242 Год назад +3

    let's consider the case after sorting
    job id deadline profit
    2 3 a[j=3] 500
    4 3 a[j=2] 400
    2 3 a[j=1] 300
    1 3 j=3 to1 200 from here you starting the j =3 loop till 1 but in array 3,2,1 index already filled
    2 5 -> 100 This is the max deadline case so in next i th iteration you filled this in array
    2 1 50
    but in this case profit, 200 skips and 100 is considered instead of 200
    this is fine ?

  • @tubanishat1139
    @tubanishat1139 Год назад

    it took me 2 days to get a grasp on problem statement l

  • @thelimit9719
    @thelimit9719 3 года назад +1

    if profits aare same, we should not sort deadline by ascending order?

    • @takeUforward
      @takeUforward  3 года назад

      Deadline wont matter since both of them will be having same profit and anyone after that will hace lesser profit.

  • @vaibhavsharma5923
    @vaibhavsharma5923 5 месяцев назад

    how to perform this sort?

  • @albedo9617
    @albedo9617 Месяц назад

    Is it wrong if we sort on the basis of deadline but for equal deadlines we sort on the basis of profit. That way we'll be able to finish the jobs that are expiring first but also accepting the jobs that have more profit first.

  • @RituSingh-ne1mk
    @RituSingh-ne1mk 5 месяцев назад

    Understood!

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx 5 месяцев назад

    Understood

  • @anishkumargiri9490
    @anishkumargiri9490 2 года назад +2

    I have done like this in C language -
    #include
    #include
    #include
    typedef enum{true,false}bool;
    typedef struct{
    char id;
    int deed;
    int profit;
    }job;
    int deed(const void *p1 , const void *p2){
    const job *pa=p1;
    const job *pb=p2;
    return pb->deed - pa->deed;
    }
    int compare(const void *p1 , const void *p2){
    const job *pa=p1;
    const job *pb=p2;
    return pb->profit - pa->profit;
    }
    int funC(int max,bool a[]){
    int i=0;
    if(max>1){
    for(i=max-1;i>=0;i--){
    if(a[i]==false){
    return i+1;
    }
    }
    }
    return 0;
    }
    int main()
    {
    job arr[]={{'a',2,40},{'b',2,30},{'c',4,20},{'d',1,70}};
    int size=sizeof(arr)/sizeof(arr[0]);
    qsort(arr,size,sizeof(arr[0]),deed);
    int max=arr[0].deed;
    qsort(arr,size,sizeof(arr[0]),compare);
    for(int i=0;i

  • @kenadave
    @kenadave Год назад

    Variables are "guys" 😄jokes apart, Thanks a lot for the content and really appreciate the hardwork 😇

  • @husler7424
    @husler7424 Год назад

    Where is brute force i.e. DP approach!???

  • @mrinmoyhalder7293
    @mrinmoyhalder7293 Год назад +1

    suppose if there are 5 jobs and all have deadline 5, and 5 is the maximum deadline among all, then for each and every deadline we've to check like this - 1+2+3+4+......(n-1) = O(n^2), isn't it

  • @sammohanty5507
    @sammohanty5507 3 года назад +5

    Hey brother
    All the best for the surgery.

  • @yeswanthh5068
    @yeswanthh5068 Год назад +1

    Understood sir

  • @ankitgarg3247
    @ankitgarg3247 2 года назад +3

    I wrote the same code.. but it is giving runtime error on GFG. Can anybody point out the mistake?
    class Solution
    {
    public:
    static bool sort_arr(Job a, Job b)
    {
    return a.profit >= b.profit;
    }
    //Function to find the maximum profit and the number of jobs done.
    vector JobScheduling(Job arr[], int n)
    {
    sort(arr, arr+n, sort_arr);
    int size = arr[0].dead;
    for(int i=1; i

    • @divyasreedev6460
      @divyasreedev6460 2 года назад +1

      For the sort_arr function, return "a.profit > b.profit" instead of "a.profit >= b.profit"

    • @ankitgarg3247
      @ankitgarg3247 2 года назад

      @@divyasreedev6460 It worked..thanks a lot :)

    • @divyasreedev6460
      @divyasreedev6460 2 года назад

      @@ankitgarg3247 You're welcome

    • @btRohit-st9jp
      @btRohit-st9jp Год назад

      @@divyasreedev6460 hey can you help me i am not getting the reason why ">=" not work if we have same profit which one come before don't matter na?

  • @pranabpaul6317
    @pranabpaul6317 Год назад

    Striver the comparison function need to be declared as static then it is running why so? please reply.
    i am also an condition if (jobCount == maxi) break; so that if jobcount is equal to maxi then no free slot is remaining so stop.

  • @abhinanda7049
    @abhinanda7049 7 месяцев назад

    understood

  • @shubhamraj5834
    @shubhamraj5834 2 года назад

    Thanks sir

  • @jitinroy2246
    @jitinroy2246 Год назад

    if(result[j]==-1){
    result[j]=arr[i].id; (replacing from result[j]=i; bcz in intuition part you said to store job id)
    count++;
    jobprofit+=arr[i].profit;
    break;
    }

  • @lakshmiprasanna7058
    @lakshmiprasanna7058 Год назад

    Understood 💯💯💯

  • @shubhamsood1406
    @shubhamsood1406 3 года назад +2

    Time complexity is O(N^2) or O(N*logN) + O(N*M) ?

    • @shashikantkumar5095
      @shashikantkumar5095 3 года назад

      Yeah i also thing, because there is one more loop inside to find the proper index

    • @karthikpaladugula5424
      @karthikpaladugula5424 3 года назад

      N logN is for sorting
      N*M is identifying the slot
      Why N^2 term??

    • @akshitarora3524
      @akshitarora3524 3 года назад +5

      @@karthikpaladugula5424 if M=N-1(Worst Case) then complexity for identifying the slot will become N*(N-1) ~N^2

    • @omkar.gaikwad
      @omkar.gaikwad 2 года назад

      max value of deadline (m) is 100, hence it will not be O(n*m)

    • @MayankLC93
      @MayankLC93 Год назад

      @@omkar.gaikwad how?

  • @gautamsaxena4647
    @gautamsaxena4647 2 месяца назад

    understood bhaiya

  • @Dharmik_Vibes
    @Dharmik_Vibes 2 месяца назад

    way of explanation is good but code is complicate. shradha khapra code is more crisp and short .and more easy to understand but dp series is amazing

  • @priyanshvatsal9791
    @priyanshvatsal9791 Год назад

    Understood 😇

  • @simrankureel94
    @simrankureel94 3 года назад

    line 36 i will start from 1

  • @parikshitrathore1510
    @parikshitrathore1510 3 года назад +1

    🔥🔥

  • @abhishekanand9430
    @abhishekanand9430 9 месяцев назад

    thanks strivers

  • @pratikshadhole6694
    @pratikshadhole6694 Год назад

    got it

  • @atharvgupta4218
    @atharvgupta4218 Год назад

    Can someone please tell me what is wrong in this code...Only 12 cases are passed class Solution
    {
    public:
    static bool comparator(Job a,Job b)
    {
    return (a.profit>b.profit);
    }
    //Function to find the maximum profit and the number of jobs done.
    vector JobScheduling(Job arr[], int n)
    {
    int maxDeadline=arr[0].dead;
    sort(arr,arr+n,comparator);
    for(int i=1;imaxDeadline)
    {
    maxDeadline=arr[i].dead;
    }
    }
    vectorv(maxDeadline+1,-1);int profit=0;
    int c=0;
    for(int i=0;i0;j--)
    {
    if(v[j]==-1)
    {
    profit+=arr[i].profit;
    v[j]=arr[i].id;
    c++;
    break;
    }
    }
    }
    vectorans;
    ans.push_back(c);
    ans.push_back(profit);
    return ans;
    }
    };

  • @pravinthakur7791
    @pravinthakur7791 3 года назад

    Needed a competitive coding partner with whom I can start my competitive coding journey .Anyone interested please reply?

  • @rabatoaa857
    @rabatoaa857 Год назад

    c++ 15:10

  • @satrap299792458
    @satrap299792458 3 года назад +3

    What if each job takes more than 1 unit of time?

    • @takeUforward
      @takeUforward  3 года назад +2

      Then from the back allocate that much time, simple!

    • @thelimit9719
      @thelimit9719 3 года назад

      "Each job takes 1 unit of time to complete" 0:25

    • @devangrajarora7002
      @devangrajarora7002 3 года назад

      ​@@takeUforward I don't think that would work in the case where unit of time taken unit. For example,
      Job 1: Profit: 50, deadline: 5, time: 5
      Job 2: Profit: 30, deadline: 5, time: 3
      Job 3: Profit: 20, deadline: 5, time: 2
      Now here we should perform Job 2 and Job 3 instead of just Job 1.

    • @yatinarora1252
      @yatinarora1252 3 года назад

      @@devangrajarora7002 I think job 1 should be done as it is having maximum profit.Simply just we will be doing job1 for continous 5 days

    • @manavshah7450
      @manavshah7450 3 года назад +1

      Just fill that many slots which is equal to the job time.
      It will work.
      And about your example question. Job 1 will be selected and that is correct.

  • @deeksha6514
    @deeksha6514 2 года назад

    On submitting this solution on gfg it's showing Runtime error for C++.

  • @yutaitadori7318
    @yutaitadori7318 3 года назад

    cool 🆒 🔥🔥🔥🔥

  • @SANDEEPKUMAR-zo5uc
    @SANDEEPKUMAR-zo5uc 2 года назад

    💘

  • @songs-pu9bq
    @songs-pu9bq 2 года назад

    🙌🙌🔥🔥

  • @ajml_hnter
    @ajml_hnter 4 месяца назад

    C++
    #include
    using namespace std;
    vector jobScheduling(vector &jobs){
    sort(jobs.begin(), jobs.end(), [](vector& a, const vector& b) {
    return a[2] > b[2];
    });
    int cnt = 0, profit = 0, n = jobs.size();
    vector take(n+1, false);
    for(auto &job : jobs){
    for(int i=job[1] - 1; i>=0; i--){
    if(!take[i]){
    take[i] = true;
    cnt++; profit += job[2];
    break;
    }
    }
    }
    return {cnt, profit};
    }

  • @jatinbhatoya8420
    @jatinbhatoya8420 2 года назад

    public static int[] JobScheduling(Job arr[], int n) {
    int ans = 0;
    int count = 0;
    int doIt[] = new int[101];
    Arrays.sort(arr, (a, b) -> Integer.compare(b.profit, a.profit));
    for (int i = 0; i < arr.length; i++) {
    int j = arr[i].deadline;
    while (j > 0 && doIt[j] != 0) j--;
    if (j > 0) {
    count++;
    ans += arr[i].profit;
    doIt[j] = arr[i].deadline;
    }
    }
    return new int[]{count, ans};
    }

  • @techtoys-tx9th
    @techtoys-tx9th 3 года назад +1

    Bhaiya please consider me back to the telegram channel , I also tried to contact to u through instagram , please bhaiya consider my request and I haven't spoke anything bad and abusive and negative through out the duration for which , I was a member of that telegram channel

    • @takeUforward
      @takeUforward  3 года назад +1

      Telegram pe text kr do, ek hi text krna mentioning unban on telegram

  • @jitenderyadav721
    @jitenderyadav721 3 года назад +1

    Bhaiya voice phat rhi hai apki

  • @cartube6219
    @cartube6219 Год назад

    bhaiya foreign me koi nhi dekhta hoga apki video😭😭😭plz plz hindi me samjhya kijiye.
    and plz code in java as well in some of your video you use only cpp and you are famous for writing code in both languages. and truly logic samajh aaya ,, lekin code nhi

    • @Ak-um1yg
      @Ak-um1yg Год назад +1

      code kudse kar bhai . aur kon video dekhega iska tension mat le

  • @binodbinod7537
    @binodbinod7537 3 года назад

    😍😍😍

  • @pawanchoure1289
    @pawanchoure1289 3 года назад

    😁

  • @shashankkumar1974
    @shashankkumar1974 3 года назад

    hello sir can we sort the array with increasing order of deadline and if deadline is same then sort it according to max profit something like this.
    struct Job
    {
    int Deadline;
    int id;
    int profit;
    }
    static bool comp(Job j1,Job j2)
    {
    if(j1.deadline!=j2.deadline)
    return j1.profit>j2.profit;
    else
    return j1.deadline

    • @kartikeyaagrawal5917
      @kartikeyaagrawal5917 3 года назад +1

      it would have been right if the question said that the person doing the job cannot complete before the deadline .here in the question it is up to the person how he shedules his work. Say in ur case task 1(deadline 1day) has some profit "a" and task 2 (deadline 2) with profit "b" & task 3 with(deadline = 2) and profit c .Say here c>b>a in such a case ur program will choose task 3 and task 1 profit as a+c; but suppose he completes task 2 on day one and task 3 on day 2 he will ear b+c which is >a+c

    • @manavshah7450
      @manavshah7450 3 года назад

      Your comparator function is wrong.
      how can you return j1.deadline

    • @aj9706
      @aj9706 2 года назад

      Why fear when Java is here use lamda function

  • @nisarggogate8952
    @nisarggogate8952 3 года назад +3

    Here is a soln with sets. Making time complexity O(NlogN). The soon by TUF was of O(N^2)
    vector JobScheduling(Job arr[], int n)
    {
    sort(arr,arr+n,compare);
    set s;
    int ans = 0,i;
    for(i=1;i

    • @takeUforward
      @takeUforward  3 года назад +4

      S.erase takes o(n),
      Refer www.cplusplus.com/reference/set/set/erase/

    • @nisarggogate8952
      @nisarggogate8952 3 года назад

      @@takeUforward ohhh I didn't know... Thank you for correcting me 🙏

    • @arshad1679
      @arshad1679 2 года назад +2

      @@nisarggogate8952I think TUF is not correct. s.erase of a range is only O(n), else it is O(logn). Check the link.

    • @MrShaundan
      @MrShaundan 2 года назад

      @take U forward
      For the first version (erase(position)), amortized constant.
      For the second version (erase(val)), logarithmic in container size.
      For the last version (erase(first,last)), linear in the distance between first and last.

    • @RaviSingh-qd5pz
      @RaviSingh-qd5pz 2 года назад

      @@takeUforward I think erasing in set using an iterator take O(1) time so it has O(nlogn) time complexity.

  • @hritikchand9930
    @hritikchand9930 2 года назад +1

    bkl ka code python mein nhi chlta

  • @dhruvmahalwar8642
    @dhruvmahalwar8642 Год назад

    what is problem with my code can someone explain
    vector JobScheduling(Job arr[], int n)
    {
    if(n==0)
    {
    return {0,0};
    }
    else if(n==1)
    {
    return {1,arr[0].profit};
    }
    int maxi=1;
    priority_queuepq;
    for(int i=0;i=1)
    {
    if(res[k]==-1||res[k]=1)
    {
    res[j-1]=res[j];
    j--;
    }
    res[k]=profit;
    res[0]=-1;
    break;
    }
    k--;
    }
    }
    int count=0;
    int maxprofit=0;
    for(int i=1;i

  • @sparshtaneja
    @sparshtaneja 2 года назад

    shoudn't it be slot[j] = arr[i].id;

  • @naveensaicremsiyadlapalli3769
    @naveensaicremsiyadlapalli3769 Год назад

    slot[j]=arr[i].id

  • @rahulgovindkumar3105
    @rahulgovindkumar3105 2 года назад +1

    Thank you bhaiya!!❤️✨

  • @codingp110
    @codingp110 21 день назад

    Understood!

  • @VikasGupta-ok9lh
    @VikasGupta-ok9lh Год назад

    Understood

  • @jitinroy2246
    @jitinroy2246 Год назад

    understood

  • @saquelainmokhtar6016
    @saquelainmokhtar6016 2 года назад

    😍