-
Notifications
You must be signed in to change notification settings - Fork 29
/
0238-Product-of-Array-Except-Self.py
41 lines (33 loc) · 1.17 KB
/
0238-Product-of-Array-Except-Self.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'''
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:
Input: [1,2,3,4]
Output: [24,12,8,6]
Note: Please solve it without division and in O(n).
Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
'''
# Using Postfix and Prefix Product
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
res = [1]*len(nums)
prefix = 1
for i in range(len(nums)):
res[i] = prefix
prefix *= nums[i]
postfix = 1
for i in range(len(nums) - 1, -1, -1):
res[i] *= postfix
postfix *= nums[i]
return res
# Similar method with small changes
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
res = [1] * (len(nums))
for i in range(1, len(nums)):
res[i] = res[i-1] * nums[i-1]
prod = 1
for i in range(len(nums) - 1, -1 ,-1):
res[i] *= prod
prod *= nums[i]
return res