Skip to content

Commit 3742f29

Browse files
author
김가은
committed
solve: product of array except self
1 parent cc399a2 commit 3742f29

File tree

1 file changed

+18
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)