Тёмный

Minimum Operations to Reduce X to Zero - Leetcode 1658 - Python 

NeetCodeIO
Подписаться 153 тыс.
Просмотров 14 тыс.
50% 1

Solving leetcode 1658, minimum operations to reduce X to Zero, today's daily leetcode problem on september 19.
🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🥷 Discord: / discord
🐦 Twitter: / neetcode1
⭐ 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:35 - Drawing Explanation
9:09 - Coding Explanation
leetcode 1658
#neetcode #leetcode #python

Опубликовано:

 

25 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 46   
@swaroopmeher2995
@swaroopmeher2995 10 месяцев назад
Damn! This is a tough solution to come up with.
@devkumar9889
@devkumar9889 10 месяцев назад
That one problem of bit masking+ dp + trees , you didn't made video on that 🙂
@paper-studio
@paper-studio 10 месяцев назад
yeah man, hard for chumps like me to solve
@Hunter-pm3xz
@Hunter-pm3xz 10 месяцев назад
There is also an nlogn solution where you can use two vectors prefix and suffix vectors and then start travelling the prefix vector from i=0 and suffix vector from j=n-1 and since these are sorted if you find a particular element of the prefix p then we can apply binary search on the suffix and vice versa there will be some edge cases for like n=1 which you have to handle
@MP-ny3ep
@MP-ny3ep 10 месяцев назад
Amazing explanation as always. Thank you
@Voffchikus
@Voffchikus 7 месяцев назад
Awesome and simple solution, thanks.
@_sonu_
@_sonu_ 10 месяцев назад
Actually it is very simple. If we use hash map O(n) Store sum as key form ending of array and pair as it's index. Then iterate from Starting and finding if x- currentSum is in dictionary. So simple
@SASA_maxillo
@SASA_maxillo 10 месяцев назад
THANK YOU SO MUCH, but his solution is more efficient since you are using a hash map the memory will be O(n) and the memory in his code is O(1)
@shoooozzzz
@shoooozzzz 5 месяцев назад
"so simple" 🙄
@infinitygod5379
@infinitygod5379 10 месяцев назад
Such a clever question. Did you come up with the Sol on your own?
@1vader
@1vader 10 месяцев назад
Instead of checking on every iteration whether the left pointer crosses the right one, you could also just check whether the target is negative once at the start and early return in that case.
@alexgrossbard6206
@alexgrossbard6206 7 месяцев назад
He actually says target < 0 is part of the reason left and right could cross when it is actually the whole reason He has a bias towards unnecessary checks in loops that felt inefficient at first but doesn't seem to affect runtime. I don't know how it plays in interviews.
@ronaldmcsidy763
@ronaldmcsidy763 10 месяцев назад
Before today I thought I knew 2 pointers. That was my only weapon in these contests. Now I realize I am a dummy!!
@ryder2674
@ryder2674 5 месяцев назад
Isn't it possible to use 2 pointers...assign the left and right to start and end respectively and from the 2 pointers take the maximum of the 2 to minimize the operations?
@avineshkrishnan9290
@avineshkrishnan9290 10 месяцев назад
Can you please solve that problem 3 days back which included bit masking + bfs
@themagickalmagickman
@themagickalmagickman 10 месяцев назад
I saw it as 2sum where you create a dictionary of prefixes and then iterate backwards and find if x-postfix exists in the prefix dictionary. Interesting problem with a lot of approaches.
@SmoothCode
@SmoothCode 10 месяцев назад
Why is it l
@HtotheG
@HtotheG 10 месяцев назад
Great question, can be confusing but note the ordering, since r always points to a valid element, in the real and valid case that l == r == len(nums - 1) it will first subtract the element from l which is the last element and is valid, l (left) will then increase out of bounds but the for loop will terminate at the top because r is at its max and we never access the element at left after we increment it potentially out of bounds. So our code may end with l == len(nums), an invalid index and right and left have crossed which we usually hate, but at that point everything is guaranteed to terminate and we never access those elements at those pointers again and thus never have to check for those conditions.
@aadil4236
@aadil4236 10 месяцев назад
Very Good explanation
@SarveshRansubhe
@SarveshRansubhe 10 месяцев назад
We can solve this using binary search on number of elements to remove.
@justintang6313
@justintang6313 10 месяцев назад
you're the goat sir ty
@catmium7974
@catmium7974 10 месяцев назад
awesome explanation
@srinathchembolu7691
@srinathchembolu7691 13 дней назад
Why cant I duplicate the array to the right and find the smallest sum subarray. It works for most of the cases but fails at a few of them? Smth like the Min No of flips to make binary string alternating?
@chien-yuyeh9386
@chien-yuyeh9386 8 месяцев назад
You’re totally awesome
@a_maxed_out_handle_of_30_chars
@a_maxed_out_handle_of_30_chars 7 месяцев назад
excellent, thank you :)
@SASA_maxillo
@SASA_maxillo 10 месяцев назад
if i took the larger number from both sides and then subtract the the larger number from x and count it as a step, will this work?
@impolitebigmac3044
@impolitebigmac3044 10 месяцев назад
no this doesn't work. For example, lets say you had x =5 and nums = [2,2,1,2,10,4], your algorithm would subtract 4 first then 2 which will never work
@SASA_maxillo
@SASA_maxillo 10 месяцев назад
@@impolitebigmac3044 oh thanks for the explanation
@adityapss2683
@adityapss2683 5 месяцев назад
That's what I did initially. Greedy approach. Failed after 45 test cases
@debajyotimajumder472
@debajyotimajumder472 10 месяцев назад
Thank you
@CS_n00b
@CS_n00b 4 месяца назад
What would the recursive solution look like?
@itsmesuraj20
@itsmesuraj20 10 месяцев назад
Hi there, actually I need a help regarding a question ,tell me how can we connect
@zhe7518
@zhe7518 9 месяцев назад
is it possible to solve this problem using a greedy algorithm? I was wondering
@pranavsharma7479
@pranavsharma7479 10 месяцев назад
damm how can i even imagine this can be done like that
@abdulgaffarabdulmalik4333
@abdulgaffarabdulmalik4333 10 месяцев назад
12:07 is the secret to sliding window
@dayashankarlakhotia4943
@dayashankarlakhotia4943 10 месяцев назад
res=MATH.MAX (res,j-i+1); one correction
@judetear7726
@judetear7726 10 месяцев назад
thank ya
@krateskim4169
@krateskim4169 10 месяцев назад
Awesome
@MasterOwen69
@MasterOwen69 10 месяцев назад
Um.. the array nums is sorted?? They never mentioned about it right??..
@rajanmaheshwari
@rajanmaheshwari 10 месяцев назад
No, I don't think the array is sorted. Check the example.
@adelinared1
@adelinared1 10 месяцев назад
Why would it be sorted?
@MasterOwen69
@MasterOwen69 10 месяцев назад
Got it 😅
@paper-studio
@paper-studio 10 месяцев назад
the blue stuff
@swastikgorai2332
@swastikgorai2332 10 месяцев назад
🙏
@dayashankarlakhotia4943
@dayashankarlakhotia4943 10 месяцев назад
class solution { public int min Operation (int []nums,int x){ int target =-x; for(num:nums) target +=num; if(target ==0) return nums.length; int sum =0,i=0,res=interger MIN_VALUE; for(int j=0;j
Далее
Longest String Chain - Leetcode 1048 - Python
15:34
Просмотров 8 тыс.
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
How To Think Like A Programmer
1:00:07
Просмотров 2 млн
Extra Characters in a String - Leetcode 2707 - Python
21:11
The Algorithm Behind Spell Checkers
13:02
Просмотров 408 тыс.
Unique Binary Search Trees II - Leetcode 95 - Python
12:51
Sliding Window Technique - Algorithmic Mental Models
36:45