Skip to content

Commit cbce8b1

Browse files
committed
feat: week3 medium 문제풀이(product-of-array-except-self)
1 parent 566f0f9 commit cbce8b1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
else:
19+
result.append(raw_product // num)
20+
21+
return result

0 commit comments

Comments
 (0)