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

Поделиться
HTML-код
  • Опубликовано: 27 окт 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 8 месяцев назад

      "so simple" 🙄

  • @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.

  • @shubhaverma5697
    @shubhaverma5697 2 месяца назад

    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 -.-)

  • @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.

  • @infinitygod5379
    @infinitygod5379 Год назад +1

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

  • @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 10 месяцев назад

    Awesome and simple solution, thanks.

  • @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

  • @zhe7518
    @zhe7518 Год назад +1

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

  • @avineshkrishnan9290
    @avineshkrishnan9290 Год назад +2

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

  • @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 8 месяцев назад

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

  • @itsmesuraj20
    @itsmesuraj20 Год назад +1

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

  • @aadil4236
    @aadil4236 Год назад

    Very Good explanation

  • @srinathchembolu7691
    @srinathchembolu7691 3 месяца назад

    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?

  • @abdulgaffarabdulmalik4333
    @abdulgaffarabdulmalik4333 Год назад

    12:07 is the secret to sliding window

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars 10 месяцев назад +1

    excellent, thank you :)

  • @debajyotimajumder472
    @debajyotimajumder472 Год назад

    Thank you

  • @chien-yuyeh9386
    @chien-yuyeh9386 11 месяцев назад

    You’re totally awesome

  • @justintang6313
    @justintang6313 Год назад

    you're the goat sir ty

  • @pranavsharma7479
    @pranavsharma7479 Год назад +2

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

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Год назад

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

  • @CS_n00b
    @CS_n00b 7 месяцев назад

    What would the recursive solution look like?

  • @MasterOwen69
    @MasterOwen69 Год назад

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

  • @judetear7726
    @judetear7726 Год назад

    thank ya

  • @krateskim4169
    @krateskim4169 Год назад

    Awesome

  • @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 Год назад

    🙏

  • @ryder2674
    @ryder2674 8 месяцев назад +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?

  • @paper-studio
    @paper-studio Год назад

    the blue stuff

  • @catmium7974
    @catmium7974 Год назад

    awesome explanation