Skip to content

Instantly share code, notes, and snippets.

@KingAshiru
Last active May 2, 2021 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KingAshiru/280b2b261ea1e801bcaf3d0bb043c00f to your computer and use it in GitHub Desktop.
Save KingAshiru/280b2b261ea1e801bcaf3d0bb043c00f to your computer and use it in GitHub Desktop.
ArrayProducts
class Solution:
def arrayProduct(self, nums):
#check for edge cases
if not nums:
return None
if len(nums) < 2:
return 0
#we create an array of 1s, which would be our final output
res = [1]*len(nums)
lprod = 1
rprod = 1
#we update our array res both ways by taking it multiples as we move along
for i in range(len(nums)):
res[i] *= lprod
lprod *= nums[i]
res[~i] *= rprod
rprod *= nums[~i]
return res
# Time O(N), Space O(N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment