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

Поділитися
Вставка
  • Опубліковано 18 лис 2024

КОМЕНТАРІ • 48

  • @swaroopmeher2995
    @swaroopmeher2995 Рік тому +10

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

  • @devkumar9889
    @devkumar9889 Рік тому +16

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

    • @paper-studio
      @paper-studio Рік тому +3

      yeah man, hard for chumps like me to solve

  • @Hunter-pm3xz
    @Hunter-pm3xz Рік тому +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

  • @_sonu_
    @_sonu_ Рік тому +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 Рік тому +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 9 місяців тому

      "so simple" 🙄

  • @shubhaverma5697
    @shubhaverma5697 3 місяці тому

    the most awesome explanation i have heard for this question! (though i nearly lost all my confidence when you said this is a generic sliding window -.-)

  • @1vader
    @1vader Рік тому +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 11 місяців тому +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.

  • @ronaldmcsidy763
    @ronaldmcsidy763 Рік тому +2

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

  • @Voffchikus
    @Voffchikus 11 місяців тому

    Awesome and simple solution, thanks.

  • @SmoothCode
    @SmoothCode Рік тому +1

    Why is it l

    • @HtotheG
      @HtotheG Рік тому +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.

  • @SarveshRansubhe
    @SarveshRansubhe Рік тому +2

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

  • @MP-ny3ep
    @MP-ny3ep Рік тому +1

    Amazing explanation as always. Thank you

  • @avineshkrishnan9290
    @avineshkrishnan9290 Рік тому +2

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

  • @itsmesuraj20
    @itsmesuraj20 Рік тому +1

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

  • @infinitygod5379
    @infinitygod5379 Рік тому +1

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

  • @srinathchembolu7691
    @srinathchembolu7691 4 місяці тому

    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?

  • @pranavsharma7479
    @pranavsharma7479 Рік тому +2

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

  • @themagickalmagickman
    @themagickalmagickman Рік тому

    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.

  • @SASA_maxillo
    @SASA_maxillo Рік тому +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 Рік тому +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 Рік тому +1

      @@impolitebigmac3044 oh thanks for the explanation

    • @adityapss2683
      @adityapss2683 9 місяців тому

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

  • @zhe7518
    @zhe7518 Рік тому +1

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

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars 11 місяців тому +1

    excellent, thank you :)

  • @aadil4236
    @aadil4236 Рік тому

    Very Good explanation

  • @justintang6313
    @justintang6313 Рік тому

    you're the goat sir ty

  • @chien-yuyeh9386
    @chien-yuyeh9386 Рік тому

    You’re totally awesome

  • @debajyotimajumder472
    @debajyotimajumder472 Рік тому

    Thank you

  • @abdulgaffarabdulmalik4333
    @abdulgaffarabdulmalik4333 Рік тому

    12:07 is the secret to sliding window

  • @CS_n00b
    @CS_n00b 8 місяців тому

    What would the recursive solution look like?

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому

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

  • @judetear7726
    @judetear7726 Рік тому

    thank ya

  • @MasterOwen69
    @MasterOwen69 Рік тому

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

  • @krateskim4169
    @krateskim4169 Рік тому

    Awesome

  • @paper-studio
    @paper-studio Рік тому

    the blue stuff

  • @ryder2674
    @ryder2674 9 місяців тому +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?

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому

    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

  • @swastikgorai2332
    @swastikgorai2332 Рік тому

    🙏

  • @catmium7974
    @catmium7974 Рік тому

    awesome explanation