Find Pivot Index - Leetcode 724 - Python

Sdílet
Vložit
  • čas přidán 5. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🐮 Support the channel: / neetcode
    Twitter: / neetcode1
    Discord: / discord
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 CODING SOLUTIONS: • Coding Interview Solut...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    🌲 TREE PLAYLIST: • Invert Binary Tree - D...
    💡 GRAPH PLAYLIST: • Course Schedule - Grap...
    💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
    💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
    💡 BINARY SEARCH PLAYLIST: • Binary Search
    📚 STACK PLAYLIST: • Stack Problems
    Python Code: github.com/neetcode-gh/leetco...
    Problem Link: leetcode.com/problems/find-pi...
    0:00 - Read the problem
    1:58 - Drawing Explanation
    7:47 - Coding Explanation
    leetcode 724
    This question was identified as a Microsoft coding interview question from here: github.com/xizhengszhang/Leet...
    #microsoft #interview #python
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • Věda a technologie

Komentáře • 47

  • @NeetCode
    @NeetCode  Před 2 lety +3

    Python Code: github.com/neetcode-gh/leetcode/blob/main/724-Find-Pivot-Index.py
    Java Code: github.com/ndesai15/coding-java/blob/master/src/com/coding/patterns/arrays/PivotIndexFinder.java
    (Provided by ndesai15 / Neel Desai)

  • @switchyard
    @switchyard Před 2 lety +85

    If I ever get a job with fanng, it was 60% me working hard and 40% watching neetcode videos

    • @leeroymlg4692
      @leeroymlg4692 Před rokem +3

      Did you end up scoring fanng job?

    • @doggo9757
      @doggo9757 Před 5 měsíci +2

      Did you end up getting a job at FAANG?

    • @SuubUWU
      @SuubUWU Před 3 měsíci +1

      How’s that FANNG job?

  • @tamaranavamankumar3890
    @tamaranavamankumar3890 Před 2 lety +3

    The way u explaining the logic, we don't need to rewind back again...great 🎉and thankyou 👍

  • @samantasunanda
    @samantasunanda Před rokem

    This is a beautiful explanation, thanks for sharing.

  • @ice_cube918
    @ice_cube918 Před 2 lety +1

    Very good explanations!

  • @leventoz9530
    @leventoz9530 Před 11 měsíci +1

    Use two arrays (of the same size as nums) to store the left sums (starting from the left) and the right sums (starting from the right) respectively. When calculating the "next" sum, you only need to add the value of the "previous" index's value with the partial sum already stored. (Next and previous's definitions depend on direction). Starting from the left, return the first index where the values are equal. Space: O(n), Time: O(n)

  • @tanmaydash803
    @tanmaydash803 Před rokem

    Thanks buddy ....loves a ton for this vdo...totally worth it

  • @skmn07
    @skmn07 Před 2 lety

    @neetcode, what software and tools u use when explaing or hilighting text or drawing text

  • @littlecatboybuddy
    @littlecatboybuddy Před rokem

    great explanation! thank you so much.

  • @vineetjadhav1785
    @vineetjadhav1785 Před 11 měsíci

    Hey Man Thanks for the smooth Explanantion ;))

  • @bollinmore
    @bollinmore Před 2 lety

    your explanation is easy to understand.

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

    Thanks for the video! I used the prefix sum methodology for this problem and it ran efficiently, sharing it in case if anyone finds it useful.
    def find_pivot_index(nums):
    prefix = [nums[0]]
    for i in range(1, len(nums)):
    prefix.append(nums[i] + prefix[-1])
    for i in range(len(prefix)):
    if i == 0:
    left_sum = 0
    else:
    left_sum = prefix[i-1]
    right_sum = prefix[-1] - prefix[i]
    if left_sum == right_sum:
    return i
    return -1

  • @sagardafle
    @sagardafle Před 2 lety +7

    Thanks.
    Isn’t the intuition behind this solution same as sliding window algo? Where we avoid re-summing the numbers.

  • @annubaba7944
    @annubaba7944 Před 2 lety +3

    Hi brother ,Could you please choose some medium-hard level dp & bitmasking kind questions . Never had a better understanding by watching many other channel videos . Hope you're gonna do it.
    Thanks!

  • @vinoddiwan5792
    @vinoddiwan5792 Před rokem +1

    Logic was to focus on non zeros numbers . But the question focused on zeros. Good question

  • @sumanbyte
    @sumanbyte Před rokem

    understood. thanks bro

  • @stupidbutcurious6206
    @stupidbutcurious6206 Před rokem

    Hi, can you please help me solve below questions please...
    The Question is:- Given an array of integers of size N, count all possible distinct triplets whose sum is exactly divisible by given integer K. for triplets i, j, k --> i

  • @rohanaurangabadkar951
    @rohanaurangabadkar951 Před 2 měsíci

    Just loved it!!!!

  • @GandharvaSMurthy
    @GandharvaSMurthy Před 2 lety +1

    Will it be easy if iterate sum from left and right together and increment or decrement left or right based on my current sum of left or right ?

    • @hamoodhabibi7026
      @hamoodhabibi7026 Před rokem +1

      problem is you don't know if there is a negative number in the future that'll destroy the sum and its not sorted

  • @MouseCodes
    @MouseCodes Před 2 lety

    best explanation

  • @jzvf_758
    @jzvf_758 Před rokem

    amazing !

  • @AseshShrestha
    @AseshShrestha Před 2 lety

    Thanks

  • @leocoote1860
    @leocoote1860 Před rokem +1

    Hi, I got a solution which is similar to yours, but instead of updating the right side by using the total left and the value of the pivot, Instead, I followed the same way we calculated the left side by taking the total right and taking away the pivot value as we move through the list (right_side -= i). Is there a reason your version is better, or are they equal roughly in speed? and Thanks for the Videos, they always help so much.

    • @DBagg-zz4ip
      @DBagg-zz4ip Před rokem

      I did the same thing and it makes negligible difference. Maybe it saves a subtract operation but I doubt it's anything but a matter of practice/taste.

  • @kaushikp918
    @kaushikp918 Před rokem

    How do actually approach the problem in order to get an idea like that? also is this dynamic programming?

  • @jzvf_758
    @jzvf_758 Před rokem

    can you do the running sum of array please. The code is pretty simple but i don't understand the for loop. would just love an explanation

    • @hamoodhabibi7026
      @hamoodhabibi7026 Před rokem

      Not sure what you mean by running sum, but basically he has a for loop and for each index in the loop we have 2 variables leftSum and rightSUm. (that keeps track of the current sum at each iteration i. of the left side and right side of i)
      So at each index of the loop when both left and rightSum equal then we know that current index is our pivot

    • @jzvf_758
      @jzvf_758 Před rokem +2

      @@hamoodhabibi7026 I mean that there is a leeetcode problem called running sum of array and I was asking him to explain it

  • @shariarnadim161
    @shariarnadim161 Před 4 dny

    How about doing it using prefix sum

  • @edwardteach2
    @edwardteach2 Před měsícem

    You a Pivot God

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

    happy teachers day

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

    class Solution {
    public int pivotIndex(int[] nums) {
    int totalSum = 0 ;
    for(int i = 0 ; i < nums.length ; i++){
    totalSum = totalSum + nums[i];
    }
    int leftSum = 0 ;
    for(int i = 0 ; i < nums.length ; i++){
    totalSum = totalSum - nums[i];
    if(leftSum == totalSum){
    return i;
    }
    leftSum = leftSum + nums[i];
    }
    return -1;
    }
    }
    Code using Java

  • @TheyCensorUsHere
    @TheyCensorUsHere Před rokem +1

    I was going insane, walked the dog, came back, easily figured it out. *Sigh*

  • @sarmadsohaib3032
    @sarmadsohaib3032 Před rokem

    Hi, please test your code with test case [-1,-1,0,1,1,0]. Expected output is 5 but your code is returning -1.

    • @NeetCode
      @NeetCode  Před rokem +1

      Just tested, and my code returns 5

    • @sarmadsohaib3032
      @sarmadsohaib3032 Před rokem

      @@NeetCode Oh Nice. I was mistaken. Thank you

  • @juda550
    @juda550 Před 2 lety

    I love you

  • @LOLjerel
    @LOLjerel Před rokem +1

    Bro this makes no sense lmao It's not even your fault.

  • @tawsifmahbub2440
    @tawsifmahbub2440 Před rokem

    If you create C++videos you will be famous more than now

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

      nope, python is much more popular than C++