Skip to content

Commit 8e4c2ba

Browse files
authored
Update dailycodingproblem
1 parent c74c603 commit 8e4c2ba

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

dailycodingproblem

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ Without division, another approach would be to first see that the ith element si
8181

8282
In order to find the product of numbers before i, we can generate a list of prefix products. Specifically, the ith element in the list would be a product of all numbers including i. Similarly, we would generate the list of suffix products.
8383
#This runs in O(N) time and space, since iterating over the input arrays takes O(N) time and creating the prefix and suffix arrays take up O(N) space.
84+
85+
#Solution by division:
86+
def products(lst):
87+
full_multiple = 1
88+
for x in lst:
89+
full_multiple *= x
90+
91+
result = []
92+
for num in lst:
93+
result.append(int(full_multiple/num))
94+
95+
return result
96+
97+
OR
98+
99+
#solution by prefix and suffix multiplication
84100
def products(nums):
85101
# Generate prefix products
86102
prefix_products = []
@@ -111,6 +127,7 @@ def products(nums):
111127
return result
112128

113129

114-
130+
#input : product([1,2,3,4,5)
131+
#output: [120, 60, 40, 30, 24]
115132

116133

0 commit comments

Comments
 (0)