From c9bdf86facd478556c193e298d8991efecd3656a Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Sat, 12 Aug 2023 09:09:03 -0700 Subject: [PATCH] Attempt using slicing to achieve O(n) without division. --- .../238_Product_of_Array_Except_Self.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/NeetCodeRoadmap/Arrays_and_Hashing/238_Product_of_Array_Except_Self.py b/NeetCodeRoadmap/Arrays_and_Hashing/238_Product_of_Array_Except_Self.py index dcf5b68..68a18d7 100644 --- a/NeetCodeRoadmap/Arrays_and_Hashing/238_Product_of_Array_Except_Self.py +++ b/NeetCodeRoadmap/Arrays_and_Hashing/238_Product_of_Array_Except_Self.py @@ -1,6 +1,15 @@ -def productExceptSelf(self, nums): +def productExceptSelf(nums): """ :type nums: List[int] :rtype: List[int] """ - \ No newline at end of file + numsProduct = [] + for x in range(len(nums)): + firstHalf = nums[:x] + secondHalf = nums[(len(nums)-x):] + if firstHalf: + product = firstHalf + secondHalf + numsProduct.append(product) + return numsProduct + +print(productExceptSelf([1,2,3,4])) \ No newline at end of file