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

Sdílet
Vložit
  • čas přidán 25. 07. 2024
  • 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

Komentáře • 46

  • @swaroopmeher2995
    @swaroopmeher2995 Před 10 měsíci +9

    Damn! This is a tough solution to come up with.

  • @devkumar9889
    @devkumar9889 Před 10 měsíci +16

    That one problem of bit masking+ dp + trees , you didn't made video on that 🙂

    • @paper-studio
      @paper-studio Před 10 měsíci +3

      yeah man, hard for chumps like me to solve

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

    Amazing explanation as always. Thank you

  • @Hunter-pm3xz
    @Hunter-pm3xz Před 10 měsíci +2

    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

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

    Awesome and simple solution, thanks.

  • @aadil4236
    @aadil4236 Před 10 měsíci

    Very Good explanation

  • @ronaldmcsidy763
    @ronaldmcsidy763 Před 10 měsíci +2

    Before today I thought I knew 2 pointers. That was my only weapon in these contests. Now I realize I am a dummy!!

  • @avineshkrishnan9290
    @avineshkrishnan9290 Před 10 měsíci +2

    Can you please solve that problem 3 days back which included bit masking + bfs

  • @justintang6313
    @justintang6313 Před 10 měsíci

    you're the goat sir ty

  • @catmium7974
    @catmium7974 Před 10 měsíci

    awesome explanation

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

    Such a clever question. Did you come up with the Sol on your own?

  • @_sonu_
    @_sonu_ Před 10 měsíci +4

    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 Před 10 měsíci +2

      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 Před 5 měsíci

      "so simple" 🙄

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

    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 Před 7 měsíci +1

      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.

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

    excellent, thank you :)

  • @ryder2674
    @ryder2674 Před 5 měsíci +1

    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?

  • @chien-yuyeh9386
    @chien-yuyeh9386 Před 8 měsíci

    You’re totally awesome

  • @debajyotimajumder472
    @debajyotimajumder472 Před 10 měsíci

    Thank you

  • @SarveshRansubhe
    @SarveshRansubhe Před 10 měsíci +2

    We can solve this using binary search on number of elements to remove.

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

    Why is it l

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

      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.

  • @themagickalmagickman
    @themagickalmagickman Před 10 měsíci

    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.

  • @srinathchembolu7691
    @srinathchembolu7691 Před 14 dny

    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?

  • @judetear7726
    @judetear7726 Před 10 měsíci

    thank ya

  • @itsmesuraj20
    @itsmesuraj20 Před 10 měsíci

    Hi there, actually I need a help regarding a question ,tell me how can we connect

  • @krateskim4169
    @krateskim4169 Před 10 měsíci

    Awesome

  • @SASA_maxillo
    @SASA_maxillo Před 10 měsíci +2

    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 Před 10 měsíci +1

      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 Před 10 měsíci +1

      @@impolitebigmac3044 oh thanks for the explanation

    • @adityapss2683
      @adityapss2683 Před 5 měsíci

      That's what I did initially. Greedy approach. Failed after 45 test cases

  • @CS_n00b
    @CS_n00b Před 4 měsíci

    What would the recursive solution look like?

  • @zhe7518
    @zhe7518 Před 9 měsíci

    is it possible to solve this problem using a greedy algorithm? I was wondering

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

    damm how can i even imagine this can be done like that

  • @abdulgaffarabdulmalik4333
    @abdulgaffarabdulmalik4333 Před 10 měsíci

    12:07 is the secret to sliding window

  • @swastikgorai2332
    @swastikgorai2332 Před 10 měsíci

    🙏

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 10 měsíci

    res=MATH.MAX (res,j-i+1);
    one correction

  • @paper-studio
    @paper-studio Před 10 měsíci

    the blue stuff

  • @MasterOwen69
    @MasterOwen69 Před 10 měsíci

    Um.. the array nums is sorted?? They never mentioned about it right??..

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 10 měsíci

    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