238. Product of Array Except Self

238. Product of Array Except Self

Code

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        res = [1]*len(nums)
        prefix = 1
        postfix = 1
        for i in range(len(nums)):
            res[i] *= prefix
            prefix *= nums[i]
        for i in range(len(nums)-1,-1,-1):
            res[i] *= postfix
            postfix *= nums[i]
        return res        

Intuition

The approach used in this code is based on the observation that the product of all the elements to the left of a given index i can be computed by taking the product of all the elements to the left of i-1 and multiplying it with the element at index i-1. Similarly, the product of all the elements to the right of index i can be computed by taking the product of all the elements to the right of i+1 and multiplying it with the element at index i+1.

Approach

  1. To implement this approach, the code first initializes an empty list res with the same length as the input list nums. It then initializes each element of res to 1. This is because, for each index i in nums, the output should be the product of all the elements in nums except for the element at index i. By initializing each element of res to 1, we can start by computing the product of all the elements to the left of i and then multiply that by the product of all the elements to the right of i.
  2. The code then initializes two variables called prefix and postfix to 1. These variables will be used to compute the product of all the elements to the left and right of each index i in nums, respectively.
  3. The first loop goes through each index i in nums and updates the corresponding element of res by multiplying it with prefix and then updating prefix to be the product of prefix and nums[i]. This computes the product of all the elements to the left of i.
  4. The second loop goes through each index i in nums in reverse order and updates the corresponding element of res by multiplying it with postfix and then updating postfix to be the product of postfix and nums[i]. This computes the product of all the elements to the right of i.
  5. Finally, the method returns the list res, which now contains the product of all the elements in nums except for each element at index i.

Time Complexity: O(n) where n is length of input list

Space Complexity: O(n) to store result

No alt text provided for this image



要查看或添加评论,请登录

Irfan Ahmed Mohammad的更多文章

  • Exploring Creational Design Patterns in Real-Life Applications: From Singleton to Prototype

    Exploring Creational Design Patterns in Real-Life Applications: From Singleton to Prototype

    Creational design patterns in Java provide ways to create objects while abstracting the instantiation process. This…

  • Exploring Structural Design Patterns in Java: From Adapter to Flyweight

    Exploring Structural Design Patterns in Java: From Adapter to Flyweight

    Introduction: In the realm of software design, Structural Design Patterns play a crucial role in organizing code that…

  • System Design Essentials: The Role of API Gateway

    System Design Essentials: The Role of API Gateway

    In the realm of system design interviews, understanding the pivotal role of API Gateways (AGs) is not just advantageous…

    2 条评论
  • System Design Essentials: The Role of Load Balancers

    System Design Essentials: The Role of Load Balancers

    In system design interviews, think of Load Balancers (LB) as gatekeepers at the entrance of a network. They're like…

    1 条评论
  • 268. Missing Number

    268. Missing Number

    Code: class Solution: def missingNumber(self, nums: List[int]) -> int: n = len(nums) res = n…

  • 190. Reverse Bits

    190. Reverse Bits

    Code class Solution: def reverseBits(self, n: int) - int: res = 0 for i in range(32):…

  • 191. Number of 1 Bits

    191. Number of 1 Bits

    Code class Solution: def hammingWeight(self, n: int) -> int: while n: n &= (n-1)…

  • 128. Longest Consecutive Sequence

    128. Longest Consecutive Sequence

    Code class Solution: def longestConsecutive(self, nums: List[int]) -> int: seen = set(nums) res = 0…

  • 49. Group Anagrams

    49. Group Anagrams

    Approach 1 Using Sorted string as key class Solution def groupAnagrams(self, strs: List[str]) -> List[List[str]]:…

    1 条评论
  • 217. Contains Duplicate

    217. Contains Duplicate

    Code class Solution def containsDuplicate(self, nums: List[int]) -> bool: hash_set = set() for item…

社区洞察

其他会员也浏览了