{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

238. Product of Array Except Self

\n", "
\n", "\n", "\n", "

Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

\n", "\n", "

Example:

\n", "\n", "
Input:  [1,2,3,4]\n",
    "Output: [24,12,8,6]\n",
    "
\n", "\n", "

Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.

\n", "\n", "

Note: Please solve it without division and in O(n).

\n", "\n", "\n", "

 

\n", "Source \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Code

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def product_except_self(nums):\n", " \"\"\"Using the following trick:\n", "\n", " nums = [a, b, c, d, e ]\n", " -----------------------------------------------\n", " L = [1, a, ab, abc, abcd]\n", " R = [bcde, cde, de, e, 1 ]\n", " -----------------------------------------------\n", " result = [bcde, acde, abde, abce, abcd]\n", " \n", " Time Complexity: O(n)\n", " Space Complexity: O(n)\n", " \"\"\"\n", " n = len(nums)\n", " L, R, output = ([1]*n for _ in range(3))\n", " # L[i] / R[i] = product of all the elements strictly to the left / right of i\n", "\n", " for i in range(1, n):\n", " L[i] = L[i-1]*nums[i-1]\n", " R[n-1-i] = R[n-i]*nums[n-i]\n", "\n", " for i in range(n):\n", " output[i] = L[i]*R[i]\n", " return output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Check

" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[24, 12, 8, 6]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nums = [1,2,3,4]\n", "product_except_self(nums)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Follow up:

\n", "

Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

\n", "\n", "

Code

" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def product_except_self(nums):\n", " \"\"\"Solution with O(1) memory (2 passes).\"\"\"\n", " n = len(nums)\n", " output = [1]*n # output = L\n", " R = 1 # we update R and build output along instead of using list\n", " for i in range(1, n):\n", " output[i] = output[i-1]*nums[i-1]\n", " for i in range(n-1, -1, -1):\n", " output[i] *= R\n", " R *= nums[i]\n", " return output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def product_except_self(nums):\n", " \"\"\"Solution with O(1) memory (1 pass).\"\"\"\n", " n = len(nums)\n", " output = [1] * n\n", " L = R = 1\n", " for i in range(n):\n", " output[i] *= L\n", " output[~i] *= R # Reminder ~i = -1-i\n", " L *= nums[i]\n", " R *= nums[~i]\n", " return output" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }