Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 238 · Prefix Sum

Product of Array Except Self

LeetCode 238 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

answer[i] = prefix_product[i] × suffix_product[i].

मध्यमArrayPrefix Sum

मी 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. 1

    First pass left→right: arr[i] = product of nums[0..i-1].

  2. 2

    Second pass right→left with a running product of nums[i+1..].

  3. 3

    Multiply the running right product into arr[i].

  4. 4

    The final version reuses the output array, so extra space is O(1).

मी कसा सोडवला · पहिल्या कल्पनेपासून अंतिम उपायापर्यंत

1
Prefix and suffix arraysO(n) time · O(n) space

Two helper arrays, multiplied at the end.

2
In-place suffix passAcceptedO(n) time · O(1) extra

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_arr
In-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 arr
स्टेप-थ्रू लोड होत आहे…

Product 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.