Minimum Difficulty of a Job Schedule - Leetcode 1335 - Python

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
    🥷 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/minimum...
    0:00 - Read the problem
    0:23 - Drawing Explanation
    8:00 - Coding Explanation
    leetcode 1335
    #neetcode #leetcode #python

Komentáře • 44

  • @walkastray007
    @walkastray007 Před 7 měsíci +1

    Hey Neetcode! I really appreciate the daily leetcode problem videos. They're such a nice way to start my day. It's sort of a ritual for me at this point to attempt a leetcode problem. I actually managed to get the correct thoughts/observations to solve the problem, but actually translating that into code was a bit tricky. As most hard problems are.
    Hope to see you tomorrow!

  • @MP-ny3ep
    @MP-ny3ep Před 7 měsíci +1

    Beautiful explanation as always. Thank you.

  • @minaFbeshay
    @minaFbeshay Před 7 měsíci

    The best to explain dynamic programming so far! Keep it bro

  • @laumatthew71
    @laumatthew71 Před 7 měsíci

    Great explanation as usual, thank you sir !

  • @aadil4236
    @aadil4236 Před 7 měsíci +1

    You are doing a really amazing and good work Navdeep. May god gives you everything you want in life. Happy New Year in advance my friend! You are a really amazing teacher! God bless you.

  • @tanmaychavan7004
    @tanmaychavan7004 Před 7 měsíci

    Thanks a bunch, was waiting for this.

  • @yang5843
    @yang5843 Před 7 měsíci

    Was hoping you would come in clutch today to solve the problem!

  • @nahidtanvir5901
    @nahidtanvir5901 Před 7 měsíci +3

    Shouldn't I calculate cur_max before checking cache?

  • @SergeKnysh
    @SergeKnysh Před 7 měsíci

    Great, all good now - thanks a lot

  • @prajjwaldubey5787
    @prajjwaldubey5787 Před 7 měsíci

    In the interview do I need to do the recursive + caching approach or the tabulation approach for any dp problem ????

  • @caleb.39
    @caleb.39 Před 7 měsíci

    Wow today's problem was so much easier than yesterday's lol. yesterday's I had spent 40 minutes trying to think about it before seeing your video. Today's problem I was able to solve it by myself in 20 minutes

  • @GameFlife
    @GameFlife Před 7 měsíci

    fast fix tks NEET!

  • @venketeshpanda5183
    @venketeshpanda5183 Před 7 měsíci +1

    And our saviour is here :D

  • @Manish-fd5jb
    @Manish-fd5jb Před 6 dny

    Just put that DP check condition after calculating cur_max; it makes a big difference.

  • @user-od5rd7ss3t
    @user-od5rd7ss3t Před 7 měsíci

    I am wondering if you can explain the monotonic stack solution as it helps learn everyone new concept. Thank you :)

  • @satwiktatikonda764
    @satwiktatikonda764 Před 7 měsíci

    if we give this memoization code in a interview will the
    interviewer be satisfied or will he ask us to optimize more and write a tabulation code?

  • @YashRekha594
    @YashRekha594 Před 7 měsíci

    I consider myself very bad at dynamic programming. I need to start from the basic DP. Please suggest where I can start and learn DP.

    • @sadiksikder7014
      @sadiksikder7014 Před 7 měsíci +1

      climbing stairs, target sum, house robber, coin change, lcs

  • @csam11100
    @csam11100 Před 7 měsíci +1

    Thanks for sharing!
    BTW, We can add the following condition in your dfs function to prune the useless states.
    remain_tasks = (n - 1) - i + 1
    if remain_tasks < d:
    return math.inf

  • @mahimanzum
    @mahimanzum Před 7 měsíci +4

    The time complexity analysis is incorrect i believe. It should be O(n*d*max_value_of_array) max value of array and length of the array are different values. Please correct me if i am wrong

    • @NeetCodeIO
      @NeetCodeIO  Před 7 měsíci +2

      Yeah you're right, but I think it would be distinct_vals_in_array

    • @yang5843
      @yang5843 Před 7 měsíci

      @@NeetCodeIO isn't max value a constant

    • @hamirmahal
      @hamirmahal Před 7 měsíci +3

      O(n*d*distinct_vals_in_array) is still O(n*d*n) because there are at most n distinct values in the array.

  • @6kostia
    @6kostia Před 7 měsíci

    you can get ~150ms for a recursive solution

  • @_PixelPioneer_
    @_PixelPioneer_ Před 7 měsíci +1

    Great Explanation but why don't you solve this problem in bottom up approach!

    • @caothanhluan1813
      @caothanhluan1813 Před 7 měsíci

      I think memoization is kinda straightforward to solve this question

    • @_PixelPioneer_
      @_PixelPioneer_ Před 7 měsíci

      @@caothanhluan1813 ya may be u are right ✅

  • @shreehari2589
    @shreehari2589 Před 7 měsíci +1

    God damn one heck of a problem this was

  • @yang5843
    @yang5843 Před 7 měsíci

    class Solution {
    Map map = new HashMap();
    public int minDifficulty(int[] jobDifficulty, int d) {
    if ( jobDifficulty.length < d ) return -1;
    return dfs(0,jobDifficulty,d,-1);
    }
    int dfs(int i, int[] jb, int d, int max) {
    if ( map.containsKey(i+" "+d+" "+max)) return map.get(i+" "+d+" "+max);
    if ( d == 0 && i == jb.length ) return 0;
    if ( d == 0 || i == jb.length ) return 10000;

    max = Math.max(max,jb[i]);
    //take
    int min = dfs(i+1,jb,d,max);

    //ignore
    min = Math.min(min,max + dfs(i+1,jb,d-1,-1));

    map.put(i+" "+d+" "+max,min);
    return min;

    }
    }

    • @chugisan6847
      @chugisan6847 Před 7 měsíci

      Is making a map-key by concatenating values safe? I mean, for i = 23, d = 4, max = 5 and for i = 2, d = 34, max = 5, the map-key would be 2345 in both the cases. I know the chances for this to happen are very slim, but I just wanted to know if this way of making map-keys is standard or it's something that you use for yourself.

    • @yang5843
      @yang5843 Před 7 měsíci

      @@chugisan6847 good question, I'm space separating the integers so that doesn't happen

    • @chugisan6847
      @chugisan6847 Před 7 měsíci

      @@yang5843 Got you! Thank you for the answer.

    • @joydeeprony89
      @joydeeprony89 Před 7 měsíci

      return 10000 , please share the logic behind magic number 10000 ?

    • @yang5843
      @yang5843 Před 7 měsíci

      @@joydeeprony89 it's an arbitrary large number, if I used max int here, there would be an overflow

  • @oijgg3p
    @oijgg3p Před 7 měsíci +1

    class Solution:
    def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
    n = len(jobDifficulty)
    if n < d:
    return -1 # Cannot schedule all jobs in d days
    memo = {}
    def dp(day, idx):
    if (day, idx) in memo:
    return memo[(day, idx)]
    if day == 1:
    # On the last day, we need to finish all remaining jobs, so return the maximum difficulty
    memo[(day, idx)] = max(jobDifficulty[idx:])
    return memo[(day, idx)]
    min_difficulty = float('inf')
    max_difficulty = 0
    # Iterate backward to find the minimum difficulty
    for i in range(idx, n - day + 1):
    max_difficulty = max(max_difficulty, jobDifficulty[i])
    rest_difficulty = dp(day - 1, i + 1)
    min_difficulty = min(min_difficulty, max_difficulty + rest_difficulty)
    memo[(day, idx)] = min_difficulty
    return min_difficulty
    result = dp(d, 0)
    return result if result != float('inf') else -1

  • @tariqelamin5847
    @tariqelamin5847 Před 7 měsíci

    First comment

  • @srirambharadwaj1937
    @srirambharadwaj1937 Před 7 měsíci

    can anyone help me with this,..why this code doesn't work. as expected
    class Solution:
    def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
    n = len(jobDifficulty)
    if n

    • @sadiksikder7014
      @sadiksikder7014 Před 7 měsíci

      how are you returning todayCost when you have not define it