Skip to content

Commit 4ee2d06

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

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# Brute Force Solution
2+
class Solution:
3+
def productExceptSelf(self, nums: List[int]) -> List[int]:
4+
n = len(nums)
5+
ans = [0] * n
6+
7+
for i in range(n):
8+
multiplier = 1
9+
for j in range(n):
10+
if i == j:
11+
continue
12+
else:
13+
multiplier *= nums[j]
14+
ans[i] = multiplier
15+
16+
return ans
17+
# Time: O(n^2)
18+
# Space: O(n)
19+
20+
21+
# Optimal Solution
122
class Solution:
223
def productExceptSelf(self, nums: List[int]) -> List[int]:
324
l_mult = 1

0 commit comments

Comments
 (0)