Maximum Performance of a Team - Leetcode 1383 - Python

Sdílet
Vložit
  • čas přidán 23. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    Problem Link: leetcode.com/problems/maximum...
    0:00 - Read the problem
    1:57 - Drawing Explanation
    11:10 - Coding Explanation
    leetcode 1383
    #coding #interview #python
  • Věda a technologie

Komentáře • 74

  • @NeetCode
    @NeetCode  Před rokem +2

    🚀 neetcode.io/ - 25% OFF LAUNCH SALE

  • @interstellar1873
    @interstellar1873 Před rokem +36

    Thanks!

  • @filipjovanovic7356
    @filipjovanovic7356 Před rokem +6

    I've got my Amazon final interview next Friday and I just wanted to let you know that I wouldn't have made it nearly as far without you. Thank you for all your videos! You have no idea how many lives you're changing

  • @mukundyadav6913
    @mukundyadav6913 Před rokem +50

    I think it would be great if you could just show us the code for the brute force way also and then go in depth to explain the optimal solution like you usually do. I personally struggle to sometimes code up even the brute force solution so learning how to do that is something I would value a lot.

    • @satyamgaba
      @satyamgaba Před rokem +2

      Writing a Brute force for this is also not easy. You'll have to make a graph that will give all the possible combination. Then you'll filter combination that has k number of enginees. There will be a function that will calculate the the result and you'll loop through all the feasible combinations

    • @hardikvora3744
      @hardikvora3744 Před rokem

      I agree. Even if you don't type it out entirely, you could just show the code and walk through it briefly!

    • @mukundyadav6913
      @mukundyadav6913 Před rokem +5

      @kaskp to...learn how to do leetcode?

    • @NeetCode
      @NeetCode  Před rokem +32

      Good suggestion, I'll try to do that in the future. For this problem it doesn't make sense, because even the brute force solution isn't trivial to code and it's mostly unrelated to the optimal solution.

    • @davidoh6342
      @davidoh6342 Před rokem

      @@NeetCode Do you think being able to solve this problem helped you in interviews? Just curious where to draw the line between actually practical leetcode questions vs great brain teaser but rarely shows up in interviews

  • @seekndstroy2560
    @seekndstroy2560 Před rokem +2

    To think I once hated dsa coding, have come a long way loving it now, all thanks to you ❤.

  • @mickyman753
    @mickyman753 Před rokem +10

    I can easily code just after half the video , he explains really good

  • @AlgoJS
    @AlgoJS Před rokem +2

    I also used heap, got to keep the daily streak going!
    Thanks for the explanation!

  • @saidud1067
    @saidud1067 Před rokem +1

    Fan of your amazing videos! Please keep adding more and more

  • @abhishekgururani6993
    @abhishekgururani6993 Před rokem

    Thank you, this is such a subtle explanation, loved it.

  • @dera_ng
    @dera_ng Před rokem

    Amazing! Elegant use of the Heap! 🙂

  • @sunilpanchal1498
    @sunilpanchal1498 Před rokem

    This guy can make hard question to easy, keep it up.

  • @StefDev
    @StefDev Před rokem +5

    You deserve 1 Mil Subscribers! 🤙

  • @mrnobodyy96
    @mrnobodyy96 Před rokem

    Great explanation, thanks!

  • @aditya-lr2in
    @aditya-lr2in Před 10 měsíci

    2 minutes into the explanation I got the idea, thanks for the video!

  • @atharvbhawsar8801
    @atharvbhawsar8801 Před rokem

    Beautiful explanation !!✨

  • @mj-lc9db
    @mj-lc9db Před rokem +3

    although i use C++, this is very well explained!!

  • @StellasAdi18
    @StellasAdi18 Před rokem

    Great. Part of neetcode community. :)

  • @supriyobanerjee1839
    @supriyobanerjee1839 Před rokem +3

    The explanation was great, but I do have a question, is it not better to pop the minimum speed from the heap only if the current speed is greater than that minimum speed?

    • @nathantheodorus
      @nathantheodorus Před rokem

      yeah just realized, if current speed is equal or lower than that minimum speed, because the efficiency will always be lower (already sorted descending), the person should be skipped entirely, like the (3,3) person in the example

    • @supriyobanerjee1839
      @supriyobanerjee1839 Před rokem

      I rethought, I guess the reason to update on each entry is to account for the fact that we need to account for all the entries until we have K entries in the heap. So instead of over complicating we can consume all the entries as they show up.

    • @dss963
      @dss963 Před rokem

      We are popping the item bcz, we need at most k engineers , and for the current engineer being processed , we need k-1 engineers visited so far whose speed is the largest among all of the other engineers previously been processed. I think this helps...

  • @hoyinli7462
    @hoyinli7462 Před rokem

    excellent explanation!

  • @fullstackwithfinn
    @fullstackwithfinn Před rokem

    awesome video :) helped me alot

  • @sidforreal
    @sidforreal Před rokem

    Oh spot...super clean explanation!!

  • @amosluesun585
    @amosluesun585 Před rokem

    amazing explanation, thank u

  • @pankajsaxena8878
    @pankajsaxena8878 Před rokem

    love from india !!!
    amazing explanation !!!!

  • @devendrarana6806
    @devendrarana6806 Před rokem

    we are popping when len(heap)==k in heap
    and adding curr value in heap..what if the current value is smaller than what we had popped?

  • @krateskim4169
    @krateskim4169 Před rokem

    very well explained

  • @suhriddeshmukh5811
    @suhriddeshmukh5811 Před rokem

    Amazing!

  • @nipunshah1373
    @nipunshah1373 Před rokem +1

    Isn't size of The Decision Tree be greater than 2^n ?? , as 2^n would be the maximum number of nodes present itself on last level ?
    (Just asking for sake of my understanding regarding that Exponential Complexity, else I understand the approach totally )

  • @KushalBhatia
    @KushalBhatia Před rokem

    Lets say if we sort the array in increasing order of their efficiency then we need two nested for loops right? Correct me if my understanding is wrong

  • @houmankamali5617
    @houmankamali5617 Před rokem +1

    Thanks for another great video! I have a question about line 11 and 12, At 14:10, when we pop the minimum value from the heap, shouldn't we first check if this minimum speed is less than the current speed i.e. spd? e.g let's say our K=1 and sorted array is like this [[10, 10], [10, 5], [10, 0]], then we should simply keep the first element as we go through the array without popping the heap.

    • @houmankamali5617
      @houmankamali5617 Před rokem

      Okay, I think I understand why that's not a problem. Let's say we remove a speed from the heap and replace it with a spd that is even smaller, now first at line 15 we don't update the res because the last value is still max. More so, let's say later we get another spd' that has a higher value than the last spd, now if this spd' is also larger than the speed which was incorrectly removed, it'll still properly lead to a higher res.

    • @venkatasriharsha4227
      @venkatasriharsha4227 Před rokem

      I think you need to read the question again. If you are considering the efficiency at ith index. You must include the speed of it. No matter even if the he popped element has greater speed than current speed.

  • @hamoodhabibi7026
    @hamoodhabibi7026 Před rokem

    can you do yesterdays daily? I'm so confused trying to track the dp array. Or buy and sell stock 3 is good too

  • @kisakye-e
    @kisakye-e Před rokem

    Thank you

  • @TechOnScreen
    @TechOnScreen Před rokem

    awesome video.. but its more of python stuff this time.. thinking of how to do it in java.

  • @aisteelmemesforaliving
    @aisteelmemesforaliving Před 10 měsíci +1

    I copied and pasted a lot of stuff and stitched together this code, but I have no idea how it works lol. I knew this could be done with bit manipulation but this method is not any better than the binary search solution, but I still wanted to try it. However, I have no idea how this works except for the msb calculation, which i modified to include (the original did not work).
    import math
    class Solution:
    def mySqrt(self, x: int) -> int:
    msb = len(str(bin(x)))
    a = 1 = 1
    return result

  • @danielsun716
    @danielsun716 Před rokem

    hello, your videos are so impressive, could you just upload the leetcode 526, beautiful arrangement? I really appreciate it.

  • @rick-kv1gl
    @rick-kv1gl Před rokem

    amazing.

  • @HyunBinKim-yo9fx
    @HyunBinKim-yo9fx Před rokem +1

    Shouldn't it be 'second *largest* value' instead of 'second smallest value' at 4:48?

  • @YT.Nikolay
    @YT.Nikolay Před rokem

    I had to watch the video until I saw: sort and which heap to use, after that I was able to solve the problem myself, but it took me 2 hrs, I am gonna cry :D

  • @theSilentPsycho
    @theSilentPsycho Před měsícem

    Same question. 2542. Maximum Subsequence Score

  • @muhammadhaseeb4096
    @muhammadhaseeb4096 Před 17 dny

    why can't I sort by speed and then maintain a pq by efficiency? at any instant in time the lowest efficiency would be at the top of pq

  • @user-nu5ff3tm9e
    @user-nu5ff3tm9e Před rokem

    AMAZING

  • @affafa100
    @affafa100 Před rokem

    Is a 0-1 Knapsack solution not possible for this? I was trying to achieve that, but was getting Wrong answer on submit.

    • @shubhamraj25
      @shubhamraj25 Před rokem

      How are you gonna store the dp array? it will be a 4d array I guess with large values, will give MLE imo

  • @girishnakate5014
    @girishnakate5014 Před rokem +1

    I think you should teach python for dsa

  • @vaibhavvashist238
    @vaibhavvashist238 Před rokem +1

    how to think of this question?

  • @faaizuddinfarooqui1809
    @faaizuddinfarooqui1809 Před 11 měsíci

    Can you do Leet Code 443 String Compression?

  • @ningzedai9052
    @ningzedai9052 Před rokem

    Hi NeetCode, can you help with 2407. Longest Increasing Subsequence II in the contest this morning? Thanks a lot!

  • @lindsayleung7825
    @lindsayleung7825 Před rokem

    I have been trying to find you on google internal, but can not find you haha. Please give me a clue

  • @dineshchintu9779
    @dineshchintu9779 Před rokem

    If u get time can you please make and upload the solution for latest weekly contest last ques(2407. Longest Increasing Subsequence II).
    Thanks in advance 😇

  • @jayeshnagarkar7131
    @jayeshnagarkar7131 Před rokem

    can someone provide java solution ?

  • @Splish_Splash
    @Splish_Splash Před rokem

    pleeease use snake_case in python ;(

  • @noormohamed8005
    @noormohamed8005 Před rokem

    Didn't understand the problem statement at all. Ty for explaining in a simple manner. Also for better readability
    eng= [[e,s] for e,s in zip(efficiency,speed)]

  • @sohambiswas8865
    @sohambiswas8865 Před rokem

    one liner: engineers = sorted(zip(efficiency, speed), reverse=True)

    • @shreehari2589
      @shreehari2589 Před rokem +2

      Reducing number of lines of code doesn’t matter, readability and efficiency matters

    • @sohambiswas8865
      @sohambiswas8865 Před rokem

      @@shreehari2589 Yes it does especially when you are using builtin python methods because they are implemented in C. If you dont believe me try benchmarking sum(range(n)) against a simple for loop.

    • @fatropolis6909
      @fatropolis6909 Před rokem

      Wow your so smart dude!! !!1!! Want a trophy?

    • @sohambiswas8865
      @sohambiswas8865 Před rokem

      @@fatropolis6909 no thanks, i already got one from your Mom last night while I taught her my “python” one-liners 😉

    • @TKNinja007
      @TKNinja007 Před 2 měsíci

      ​@@shreehari2589 This is readable for any python developers and is definitely efficient

  • @jayeshnagarkar7131
    @jayeshnagarkar7131 Před rokem +1

    can someone help in my java code? Some test cases are not getting passed.Thank you.
    class Solution {
    public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
    int[][] zip = new int[speed.length][2]; //eff,speed
    for(int i = 0; i < speed.length; i++){
    zip[i] = new int[] {efficiency[i], speed[i]};
    }
    Arrays.sort(zip, (a,b) -> Integer.compare(b[0], a[0]));
    Queue minHeap = new PriorityQueue((a,b) -> Integer.compare(a,b));
    int res = 0;
    int totalSpeed = 0;
    for(int[] pair : zip){
    if(minHeap.size() == k){
    totalSpeed -= minHeap.poll();
    }
    int eff = pair[0], spd = pair[1];
    minHeap.add(spd);
    totalSpeed += spd;
    res = Math.max(res, totalSpeed*eff);
    }
    return (int)(res % (Math.pow(10,9) + 7));
    }
    }

  • @shipraverma7263
    @shipraverma7263 Před rokem

    Thanks for the intution, I solved this using 2 Priority queues
    int maxPerformance(int n, vector& speed, vector& eff, int k) {
    int mod=1e9+7;
    priority_queue pq;
    for(int i=0;ik){
    sum-=minheap.top();
    minheap.pop();
    }
    res=max(res,pq.top().first*sum);
    pq.pop();
    }
    return res%mod;
    }