Skip to content

Commit 43ba99c

Browse files
author
jinbeom
committed
Product of Array Except Self Solution
1 parent 870ef38 commit 43ba99c

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+
# 시간복잡도: O(N)
2+
# 공간복잡도: out 제외시 O(1)
3+
class Solution:
4+
def productExceptSelf(self, nums: List[int]) -> List[int]:
5+
n = len(nums)
6+
out = [1] * n
7+
8+
prod = 1
9+
for i in range(n - 1):
10+
prod *= nums[i]
11+
out[i + 1] *= prod
12+
13+
prod = 1
14+
for i in range(n - 1, 0, -1):
15+
prod *= nums[i]
16+
out[i - 1] *= prod
17+
18+
return out

0 commit comments

Comments
 (0)