Find Pivot Index - Leetcode 724 - Python
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
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)
If I ever get a job with fanng, it was 60% me working hard and 40% watching neetcode videos
Did you end up scoring fanng job?
Did you end up getting a job at FAANG?
How’s that FANNG job?
The way u explaining the logic, we don't need to rewind back again...great 🎉and thankyou 👍
This is a beautiful explanation, thanks for sharing.
Very good explanations!
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)
Thanks buddy ....loves a ton for this vdo...totally worth it
@neetcode, what software and tools u use when explaing or hilighting text or drawing text
great explanation! thank you so much.
Hey Man Thanks for the smooth Explanantion ;))
your explanation is easy to understand.
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
Thanks.
Isn’t the intuition behind this solution same as sliding window algo? Where we avoid re-summing the numbers.
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!
Logic was to focus on non zeros numbers . But the question focused on zeros. Good question
understood. thanks bro
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
Just loved it!!!!
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 ?
problem is you don't know if there is a negative number in the future that'll destroy the sum and its not sorted
best explanation
amazing !
Thanks
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.
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.
How do actually approach the problem in order to get an idea like that? also is this dynamic programming?
Search for "sliding window"
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
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
@@hamoodhabibi7026 I mean that there is a leeetcode problem called running sum of array and I was asking him to explain it
How about doing it using prefix sum
You a Pivot God
happy teachers day
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
I was going insane, walked the dog, came back, easily figured it out. *Sigh*
Hi, please test your code with test case [-1,-1,0,1,1,0]. Expected output is 5 but your code is returning -1.
Just tested, and my code returns 5
@@NeetCode Oh Nice. I was mistaken. Thank you
I love you
Bro this makes no sense lmao It's not even your fault.
If you create C++videos you will be famous more than now
nope, python is much more popular than C++