We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 566f0f9 commit cbce8b1Copy full SHA for cbce8b1
product-of-array-except-self/jinah92.py
@@ -0,0 +1,21 @@
1
+# O(n) time, O(n) space
2
+class Solution:
3
+ def productExceptSelf(self, nums: List[int]) -> List[int]:
4
+ non_zero_product = math.prod(filter(lambda x: x != 0, nums))
5
+ raw_product = math.prod(nums)
6
+ zero_total = nums.count(0)
7
+
8
+ result = []
9
10
+ for num in nums:
11
+ if zero_total > 1:
12
+ result.append(0)
13
+ elif zero_total == 1:
14
+ if num == 0:
15
+ result.append(non_zero_product)
16
+ else:
17
+ result.append(raw_product)
18
19
+ result.append(raw_product // num)
20
21
+ return result
0 commit comments