Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 238 · Prefix Sum

Product of Array Except Self

LeetCode 238 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

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

நடுத்தரம்ArrayPrefix Sum
LeetCode இல் திற ↗

நான் 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.