LeetCode #238: Product of Array Except Self | Prefix Sum

preview_player
Показать описание
0:00 Problem overview
0:27 Brute force solution
1:22 Optimized O(n) solution
4:22 Code walkthrough

#programming #coding #algorithms
Рекомендации по теме
Комментарии
Автор

I really appreciate the explanation and how you visualize the problem! Great stuff!

karlagabriel
Автор

Very clear concise explanation and visualisation! Thank you very much and please keep posting! You are highly appreciated!

dnm
Автор

the explanation of the logic is just fantastic!!

Sanai
Автор

Once again, best visuals and explanation out there!

jpkeys
Автор

Once again, best explanation and visuals out there, thank you!

jpkeys
Автор

the grid visualization really helped. finally understood this problem thank you

yilinliu
Автор

perfect . simplified .really easy to understand .kudos man . cant wait for more

bullymaguire
Автор

Though I could understand what the solutions were doing there was not much for the intuition behind it. This puts that crucial piece of the puzzle together. Thank you

ChamplooMusashi
Автор

Great explanation, i ha to watch it 2 times, but i understood it

voicubogdan
Автор

My only question is how do you even start coming up with an algorithm to solve it, i understand the solution but coming up with the solution in the first place is difficult for me, how do you think to start doing left and right products and multiply them?

azka
Автор

how on earth you came up with this solution omg :D

ruslan
Автор

thanks for this, and thanks for the captions.

skinnycucumber
Автор

Your explanation is really good but when u fill up the left and right, ur red line of box is not easy to understand. Instead of that, just say multiply every left side or multiply every right side elements. Not like one by one.
For example, when we calculate every left side of 5, say 2*3*4.
And I think until 4:12 mins are enough to understand this question.

Thank you for your explanation.

cocotv
Автор

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
i = 0
length = len(nums)
j = length - 1
left = list([1]);right = list([1])

for i in range(length-1):
left.append( left[i] * nums[i] )
right.append( right[i] * nums[j] )
# backwards
j -= 1

right.reverse()
out = [ i * j for i, j in zip(left, right) ]
return out

guruprasaad
Автор

Could you also find the product of all elements, and divide by each num[i] to create the product array

runesbroken
Автор

Could you use C++, Java or C#, please? Python is hard to read.

tomdriver
Автор

But it's gonna be O(2n-2), right?

Khanhan
Автор

My Solution
nums = [3, 2, 4, 0]

res=[]
nums1=copy.copy(nums)
print(nums1)
for i in range(0, len(nums1)):
nums1.remove(nums[i])
prod=reduce((lambda x, y:x*y), nums1)
res.append(prod)
nums1.insert(i, nums[i])
return(res)

DevanshClasses