Last active
May 2, 2021 17:07
-
-
Save KingAshiru/280b2b261ea1e801bcaf3d0bb043c00f to your computer and use it in GitHub Desktop.
ArrayProducts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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