Minimum Difficulty of a Job Schedule - Leetcode 1335 - Python
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
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!
Beautiful explanation as always. Thank you.
The best to explain dynamic programming so far! Keep it bro
Great explanation as usual, thank you sir !
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.
Thanks a bunch, was waiting for this.
Was hoping you would come in clutch today to solve the problem!
Shouldn't I calculate cur_max before checking cache?
Great, all good now - thanks a lot
In the interview do I need to do the recursive + caching approach or the tabulation approach for any dp problem ????
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
fast fix tks NEET!
And our saviour is here :D
Just put that DP check condition after calculating cur_max; it makes a big difference.
I am wondering if you can explain the monotonic stack solution as it helps learn everyone new concept. Thank you :)
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?
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.
climbing stairs, target sum, house robber, coin change, lcs
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
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
Yeah you're right, but I think it would be distinct_vals_in_array
@@NeetCodeIO isn't max value a constant
O(n*d*distinct_vals_in_array) is still O(n*d*n) because there are at most n distinct values in the array.
you can get ~150ms for a recursive solution
Great Explanation but why don't you solve this problem in bottom up approach!
I think memoization is kinda straightforward to solve this question
@@caothanhluan1813 ya may be u are right ✅
God damn one heck of a problem this was
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;
}
}
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.
@@chugisan6847 good question, I'm space separating the integers so that doesn't happen
@@yang5843 Got you! Thank you for the answer.
return 10000 , please share the logic behind magic number 10000 ?
@@joydeeprony89 it's an arbitrary large number, if I used max int here, there would be an overflow
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
First comment
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
how are you returning todayCost when you have not define it