Find Pivot Index - Leetcode 724 - Python

preview_player
Показать описание


0:00 - Read the problem
1:58 - Drawing Explanation
7:47 - Coding Explanation

leetcode 724

#microsoft #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

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

switchyard
Автор

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

gowthamjs
Автор

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

tamaranavamankumar
Автор

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)

leventoz
Автор

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

vinoddiwan
Автор

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<j<k

stupidbutcurious
Автор

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

sagardafle
Автор

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!

annubaba
Автор

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

tanmaydash
Автор

This is a beautiful explanation, thanks for sharing.

samantasunanda
Автор

your explanation is easy to understand.

bollinmore
Автор

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.

leocoote
Автор

class Solution {
public int pivotIndex(int[] nums) {
int total = Arrays.stream(nums).sum();
int leftSum = 0;
for(int i=0;i<nums.length;i++){
int rightSum = total - leftSum - nums[i];
if(leftSum == rightSum){
return i;
}
leftSum = leftSum + nums[i];
}
return -1;
}
}

setiyakitchen
Автор

Hey Man Thanks for the smooth Explanantion ;))

vineetjadhav
Автор

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

inspiredomkar
Автор

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 ?

GandharvaSMurthy
Автор

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

skmn
Автор

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

kaushikp
Автор

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

jzvf_
Автор

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

sarmadsohaib