Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions data_structures/arrays/ProductOfArrayExceptSelf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from typing import List

Check failure on line 1 in data_structures/arrays/ProductOfArrayExceptSelf.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

data_structures/arrays/ProductOfArrayExceptSelf.py:1:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 1 in data_structures/arrays/ProductOfArrayExceptSelf.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/arrays/ProductOfArrayExceptSelf.py:1:1: N999 Invalid module name: 'ProductOfArrayExceptSelf'
import doctest

Check failure on line 2 in data_structures/arrays/ProductOfArrayExceptSelf.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

data_structures/arrays/ProductOfArrayExceptSelf.py:1:1: I001 Import block is un-sorted or un-formatted


class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:

Check failure on line 6 in data_structures/arrays/ProductOfArrayExceptSelf.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/ProductOfArrayExceptSelf.py:6:53: UP006 Use `list` instead of `List` for type annotation

Check failure on line 6 in data_structures/arrays/ProductOfArrayExceptSelf.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/ProductOfArrayExceptSelf.py:6:39: UP006 Use `list` instead of `List` for type annotation

Check failure on line 6 in data_structures/arrays/ProductOfArrayExceptSelf.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

data_structures/arrays/ProductOfArrayExceptSelf.py:6:9: N802 Function name `productExceptSelf` should be lowercase
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: productExceptSelf

"""
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].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

Check failure on line 11 in data_structures/arrays/ProductOfArrayExceptSelf.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/ProductOfArrayExceptSelf.py:11:89: E501 Line too long (93 > 88)

This solution runs in O(n) time and O(1) extra space (excluding output),
without using division.

>>> Solution().productExceptSelf([1, 2, 3, 4])
[24, 12, 8, 6]
>>> Solution().productExceptSelf([-1, 1, 0, -3, 3])
[0, 0, 9, 0, 0]
>>> Solution().productExceptSelf([0, 0, 0])
[0, 0, 0]
>>> Solution().productExceptSelf([0, 1, 2, 3])
[6, 0, 0, 0]
>>> Solution().productExceptSelf([0, 0, 1])
[0, 0, 0]
>>> Solution().productExceptSelf([-1, -2, -3])
[6, 3, 2]
>>> Solution().productExceptSelf([1, 2])
[2, 1]
>>> Solution().productExceptSelf([1, 1, 1])
[1, 1, 1]
>>> Solution().productExceptSelf([-30, 30, -30, 30])
[-27000, 27000, -27000, 27000]
>>> Solution().productExceptSelf([5, 0, 0, 5])
[0, 0, 0, 0]
"""
n = len(nums)
answer = [1] * n

# Left pass: Compute prefix products
left_product = 1
for i in range(n):
answer[i] *= left_product
left_product *= nums[i]

# Right pass: Compute suffix products and multiply into answer
right_product = 1
for i in range(n - 1, -1, -1):
answer[i] *= right_product
right_product *= nums[i]

return answer


# #optimized Answer
# class Solution(object):
# def productExceptSelf(self, nums):
# m = 1
# z = 0
# for i in nums:
# if i !=0:
# m*= i
# else:
# z+=1

# if z==1:
# return [m if i==0 else 0 for i in nums]
# if z>1:
# return [0]*len(nums)
# return [m//i for i in nums]


if __name__ == "__main__":
# Run doctests
doctest.testmod()

# Additional manual execution example
sol = Solution()
test_cases = [
[1, 2, 3, 4],
[-1, 1, 0, -3, 3],
[0, 0, 0],
[0, 1, 2, 3],
[0, 0, 1],
[-1, -2, -3],
[1, 2],
[1, 1, 1],
[-30, 30, -30, 30],
[5, 0, 0, 5],
]

print("\nRunning additional test cases:")
for case in test_cases:
result = sol.productExceptSelf(case)
print(f"Input: {case} -> Output: {result}")
Loading