Skip to content

Commit 5fba7d1

Browse files
committed
product-of-array-except-self solution
1 parent 68fc9a2 commit 5fba7d1

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
"""
4+
time complexity : O(n)
5+
space complexity : O(n)
6+
"""
7+
n = len(nums)
8+
prefix = [1] * n
9+
suffix = [1] * n
10+
answer = [1] * n
11+
12+
for i in range(1, n):
13+
prefix[i] = prefix[i-1] * nums[i-1]
14+
15+
for i in range(n-2, -1, -1):
16+
suffix[i] = suffix[i+1] * nums[i+1]
17+
18+
for i in range(n):
19+
answer[i] = prefix[i] * suffix[i]
20+
21+
return answer

0 commit comments

Comments
 (0)