From c4be1896876536b7f52b7a4edc0d29826df0c596 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Thu, 17 Aug 2023 02:00:30 -0700 Subject: [PATCH] Completes 18/22 timed out --- .../238_Product_of_Array_Except_Self.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 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 2ecf106..97adbdb 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,16 +1,19 @@ +from functools import reduce +import operator + + def productExceptSelf(nums): """ :type nums: List[int] :rtype: List[int] """ - numsProduct = [] + final = [] for x in range(len(nums)): - firstHalf = nums[:x] - secondHalf = nums[(len(nums)-x):] - if firstHalf: - product = firstHalf + secondHalf - numsProduct.append(product) - - return numsProduct - + temp = nums[0] + del nums[0] + final.append(reduce(operator.mul, nums)) + # print(nums) + nums.append(temp) + # print(final) + return final print(productExceptSelf([1,2,3,4])) \ No newline at end of file