Skip to content

Commit f2b91b1

Browse files
committed
feat: add Product of Array Except Self
1 parent bdd3051 commit f2b91b1

File tree

7 files changed

+225
-1
lines changed

7 files changed

+225
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"problem_name": "product_of_array_except_self",
3+
"solution_class_name": "Solution",
4+
"problem_number": "238",
5+
"problem_title": "Product of Array Except Self",
6+
"difficulty": "Medium",
7+
"topics": "Array, Prefix Sum",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given an integer array `nums`, return an array `answer` such that `answer[i]` is equal to the product of all the elements of `nums` except `nums[i]`.\n\nThe product of any prefix or suffix of `nums` is guaranteed to fit in a 32-bit integer.\n\nYou must write an algorithm that runs in O(n) time and without using the division operation.",
10+
"readme_examples": [
11+
{ "content": "```\nInput: nums = [1,2,3,4]\nOutput: [24,12,8,6]\n```" },
12+
{ "content": "```\nInput: nums = [-1,1,0,-3,3]\nOutput: [0,0,9,0,0]\n```" }
13+
],
14+
"readme_constraints": "- 2 <= nums.length <= 10^5\n- -30 <= nums[i] <= 30\n- The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer.",
15+
"readme_additional": "**Follow up:** Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)",
16+
"solution_imports": "",
17+
"solution_methods": [
18+
{
19+
"name": "product_except_self",
20+
"parameters": "nums: list[int]",
21+
"return_type": "list[int]",
22+
"dummy_return": "[]"
23+
}
24+
],
25+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
26+
"test_class_name": "ProductOfArrayExceptSelf",
27+
"test_helper_methods": [
28+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
29+
],
30+
"test_methods": [
31+
{
32+
"name": "test_product_except_self",
33+
"parametrize": "nums, expected",
34+
"parametrize_typed": "nums: list[int], expected: list[int]",
35+
"test_cases": "[([1, 2, 3, 4], [24, 12, 8, 6]), ([-1, 1, 0, -3, 3], [0, 0, 9, 0, 0]), ([2, 3, 4, 5], [60, 40, 30, 24])]",
36+
"body": "result = self.solution.product_except_self(nums)\nassert result == expected"
37+
}
38+
],
39+
"playground_imports": "from solution import Solution",
40+
"playground_test_case": "# Example test case\nnums = [1, 2, 3, 4]\nexpected = [24, 12, 8, 6]",
41+
"playground_execution": "result = Solution().product_except_self(nums)\nresult",
42+
"playground_assertion": "assert result == expected"
43+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= add_binary
2+
PROBLEM ?= product_of_array_except_self
33
FORCE ?= 0
44
COMMA := ,
55

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Product of Array Except Self
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Prefix Sum
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 238](https://leetcode.com/problems/product-of-array-except-self/description/)
8+
9+
## Problem Description
10+
11+
Given an integer array `nums`, return an array `answer` such that `answer[i]` is equal to the product of all the elements of `nums` except `nums[i]`.
12+
13+
The product of any prefix or suffix of `nums` is guaranteed to fit in a 32-bit integer.
14+
15+
You must write an algorithm that runs in O(n) time and without using the division operation.
16+
17+
## Examples
18+
19+
### Example 1:
20+
21+
```
22+
Input: nums = [1,2,3,4]
23+
Output: [24,12,8,6]
24+
```
25+
26+
### Example 2:
27+
28+
```
29+
Input: nums = [-1,1,0,-3,3]
30+
Output: [0,0,9,0,0]
31+
```
32+
33+
## Constraints
34+
35+
- 2 <= nums.length <= 10^5
36+
- -30 <= nums[i] <= 30
37+
- The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer.
38+
39+
**Follow up:** Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)

leetcode/product_of_array_except_self/__init__.py

Whitespace-only changes.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "setup",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"nums = [1, 2, 3, 4]\n",
22+
"expected = [24, 12, 8, 6]"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"id": "execute",
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"data": {
33+
"text/plain": [
34+
"[24, 12, 8, 6]"
35+
]
36+
},
37+
"execution_count": 3,
38+
"metadata": {},
39+
"output_type": "execute_result"
40+
}
41+
],
42+
"source": [
43+
"result = Solution().product_except_self(nums)\n",
44+
"result"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"id": "test",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"assert result == expected"
55+
]
56+
}
57+
],
58+
"metadata": {
59+
"kernelspec": {
60+
"display_name": "leetcode-py-py3.13",
61+
"language": "python",
62+
"name": "python3"
63+
},
64+
"language_info": {
65+
"codemirror_mode": {
66+
"name": "ipython",
67+
"version": 3
68+
},
69+
"file_extension": ".py",
70+
"mimetype": "text/x-python",
71+
"name": "python",
72+
"nbconvert_exporter": "python",
73+
"pygments_lexer": "ipython3",
74+
"version": "3.13.7"
75+
}
76+
},
77+
"nbformat": 4,
78+
"nbformat_minor": 5
79+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
# Time: O(n)
3+
# Space: O(1)
4+
def product_except_self(self, nums: list[int]) -> list[int]:
5+
# Example: nums = [1, 2, 3, 4]
6+
# Expected output: [24, 12, 8, 6]
7+
8+
n = len(nums)
9+
result = [1] * n # [1, 1, 1, 1]
10+
11+
# Left pass: result[i] = product of all elements to the left of i
12+
# nums: [1, 2, 3, 4]
13+
# result: [1, 1, 2, 6] (left products)
14+
for i in range(1, n):
15+
result[i] = result[i - 1] * nums[i - 1]
16+
17+
# Right pass: multiply by product of all elements to the right of i
18+
# right products: [24, 12, 4, 1]
19+
# result: [1*24, 1*12, 2*4, 6*1] = [24, 12, 8, 6]
20+
right = 1
21+
for i in range(n - 1, -1, -1):
22+
result[i] *= right
23+
right *= nums[i]
24+
25+
return result
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .solution import Solution
6+
7+
8+
class TestProductOfArrayExceptSelf:
9+
def setup_method(self):
10+
self.solution = Solution()
11+
12+
@pytest.mark.parametrize(
13+
"nums, expected",
14+
[
15+
([1, 2, 3, 4], [24, 12, 8, 6]),
16+
([-1, 1, 0, -3, 3], [0, 0, 9, 0, 0]),
17+
([2, 3, 4, 5], [60, 40, 30, 24]),
18+
# Edge cases
19+
([1, 1], [1, 1]), # Minimum length
20+
([5, 2], [2, 5]), # Two elements
21+
([0, 0], [0, 0]), # All zeros
22+
([1, 0], [0, 1]), # Single zero
23+
([0, 1, 2], [2, 0, 0]), # Zero at start
24+
([1, 2, 0], [0, 0, 2]), # Zero at end
25+
# Negative numbers
26+
([-1, -2], [-2, -1]),
27+
([-1, -2, -3], [6, 3, 2]),
28+
([1, -2, 3], [-6, 3, -2]),
29+
# All ones
30+
([1, 1, 1, 1], [1, 1, 1, 1]),
31+
# Large numbers
32+
([10, 3, 5, 6, 2], [180, 600, 360, 300, 900]),
33+
],
34+
)
35+
@logged_test
36+
def test_product_except_self(self, nums: list[int], expected: list[int]):
37+
result = self.solution.product_except_self(nums)
38+
assert result == expected

0 commit comments

Comments
 (0)