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 68fc9a2 commit 5fba7d1Copy full SHA for 5fba7d1
1 file changed
product-of-array-except-self/hyeri0903.py
@@ -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