Dev Ops in 30 Days

238. Product of Array Except Self

238. Product of Array Except Self

Goal: Create in-place (O(1) Memory) O(n) time complexity solution
Method:
note that for every element output[j] = (nums[0] * ... * nums[j - 1]) * (nums[j + 1] * ... nums[n - 1])

Based on this operation, we can calculate output[j] by calculating the left part  (nums[0] * ... * nums[j - 1])  first, then right part (nums[j + 1] * ... nums[n - 1]), thus produce the code below:

content

Comments