Skip to content

Commit 310dc1c

Browse files
authored
Update Product of Array Except Self - Leetcode 238.py
1 parent 4ee2d06 commit 310dc1c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,33 @@ def productExceptSelf(self, nums: List[int]) -> List[int]:
3838

3939
# Time Complexity: O(n)
4040
# Space Complexity: O(n)
41+
42+
43+
# Optimal Solution for Bootcamp
44+
class Solution:
45+
def productExceptSelf(self, nums: List[int]) -> List[int]:
46+
n = len(nums)
47+
answer = [0] * n
48+
49+
L = 1
50+
left_product = [0] * n
51+
52+
for i in range(n):
53+
left_product[i] = L
54+
L *= nums[i]
55+
56+
57+
R = 1
58+
right_product = [0] * n
59+
60+
for i in range(n-1, -1, -1):
61+
right_product[i] = R
62+
R *= nums[i]
63+
64+
65+
for i in range(n):
66+
answer[i] = left_product[i] * right_product[i]
67+
68+
return answer
69+
# Time Complexity: O(n)
70+
# Space Complexity: O(n)

0 commit comments

Comments
 (0)