Product of Array Except Self
LeetCode 238 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్త్రూ
answer[i] = prefix_product[i] × suffix_product[i].
నేను Product of Array Except Self (LeetCode 238) ను ఇలా పరిష్కరించాను — Prefix Sum ప్యాటర్న్లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.
కింది వివరణ ఇంగ్లీష్లో ఉంది.
Intuition
The answer at i is (product of everything left of i) × (product of everything right of i). Build the left products in one pass and the right products in a second pass — no division needed, so zeros are handled naturally.
Approach
- 1
First pass left→right: arr[i] = product of nums[0..i-1].
- 2
Second pass right→left with a running product of nums[i+1..].
- 3
Multiply the running right product into arr[i].
- 4
The final version reuses the output array, so extra space is O(1).
నేను ఎలా పరిష్కరించాను · మొదటి ఆలోచన నుంచి చివరి పరిష్కారం వరకు
Two helper arrays, multiplied at the end.
Fold the suffix product into the output.
Prefix and suffix arraysPythonO(n) time · O(n) space
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
"""
prefix arr - 1,1,2,6
suffix arr - 24,12,4,1
"""
n = len(nums)
prefix_arr = [1]*n
suffix_arr = [1]*n
left = 1
for i in range(1,n):
left = left*nums[i-1]
prefix_arr[i]=left
right = 1
for i in range(n-2,-1,-1):
right = right*nums[i+1]
suffix_arr[i]=right
for i in range(n):
prefix_arr[i]=prefix_arr[i]*suffix_arr[i]
return prefix_arrIn-place suffix passPythonO(n) time · O(1) extra
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
"""
prefix arr - 1,1,2,6
suffix arr - 24,12,4,1
"""
n = len(nums)
arr = [1]*n
left = 1
for i in range(1,n):
left = left*nums[i-1]
arr[i]=left
right = 1
for i in range(n-2,-1,-1):
right = right*nums[i+1]
arr[i]=arr[i]*right
return arrProduct of Array Except Self — తరచూ అడిగే ప్రశ్నలు
Product of Array Except Self వెనుక ఉన్న intuition ఏమిటి?
The answer at i is (product of everything left of i) × (product of everything right of i). Build the left products in one pass and the right products in a second pass — no division needed, so zeros are handled naturally.
Product of Array Except Self (LeetCode 238) ను ఎలా పరిష్కరించాలి?
1. First pass left→right: arr[i] = product of nums[0..i-1]. 2. Second pass right→left with a running product of nums[i+1..]. 3. Multiply the running right product into arr[i]. 4. The final version reuses the output array, so extra space is O(1).
Product of Array Except Self యొక్క time, space complexity ఎంత?
Time: O(n). Space: O(1) extra (output excluded). (Accepted approach: In-place suffix pass.)
Product of Array Except Self లో సాధారణ పొరపాట్లు ఏమిటి?
Dividing the total product breaks on zeros.
Product of Array Except Self ను పరిష్కరించడానికి ఒకటి కంటే ఎక్కువ మార్గాలు ఉన్నాయా?
Prefix and suffix arrays (O(n) time · O(n) space): Two helper arrays, multiplied at the end. In-place suffix pass (O(n) time · O(1) extra): Fold the suffix product into the output.