diff --git a/src/my_project/interviews/top_150_questions_round_23/ex_13_product_of_array_except_itself.py b/src/my_project/interviews/top_150_questions_round_23/ex_13_product_of_array_except_itself.py new file mode 100644 index 00000000..85d984ed --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_23/ex_13_product_of_array_except_itself.py @@ -0,0 +1,36 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + """ + Calculate product of all elements except self without division. + + Strategy: Two-pass with prefix and suffix products + - First pass: Build prefix products (product of all elements to the left) + - Second pass: Build suffix products (product of all elements to the right) + - Result[i] = prefix[i] * suffix[i] + + Optimization: Use output array to store prefix, then multiply by suffix in-place + + Time: O(n), Space: O(1) excluding output array + """ + + n = len(nums) + answer = [1] * n + + # First pass: Calculate prefix products + # answer[i] contains product of all elements to the left of i + prefix = 1 + for i in range(n): + answer[i] = prefix + prefix *= nums[i] + + # Second pass: Calculate suffix products and multiply with prefix + # For each position, multiply existing prefix with product of all elements to the right + suffix = 1 + for i in range(n - 1, -1, -1): + answer[i] *= suffix + suffix *= nums[i] + + return answer \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_23/ex_13_product_of_array_except_itself.ts b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_13_product_of_array_except_itself.ts new file mode 100644 index 00000000..b69ecc90 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_13_product_of_array_except_itself.ts @@ -0,0 +1,20 @@ +function productExceptSelf(nums: number[]): number[] { + const n = nums.length; + const answer: number[] = new Array(n).fill(1); + + // First pass: prefix products + let prefix = 1; + for (let i = 0; i < n; i++) { + answer[i] = prefix; + prefix *= nums[i]; + } + + // Second pass: suffix products multiplied in-place + let suffix = 1; + for (let i = n - 1; i >= 0; i--) { + answer[i] *= suffix; + suffix *= nums[i]; + } + + return answer; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_23/test_13_product_of_array_except_itself_round_23.py b/tests/test_150_questions_round_23/test_13_product_of_array_except_itself_round_23.py new file mode 100644 index 00000000..0c15f7fb --- /dev/null +++ b/tests/test_150_questions_round_23/test_13_product_of_array_except_itself_round_23.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_23\ +.ex_13_product_of_array_except_itself import Solution + +class ProductExcetItselfTestCase(unittest.TestCase): + + def test_product_first_case(self): + solution = Solution() + output = solution.productExceptSelf(nums = [1,2,3,4]) + target = [24,12,8,6] + self.assertEqual(output, target) + + def test_product_second_case(self): + solution = Solution() + output = solution.productExceptSelf(nums = [-1,1,0,-3,3]) + target = [0,0,9,0,0] + self.assertEqual(output, target) \ No newline at end of file