refer to: https://www.algoexpert.io/questions/Array%20Of%20Products


Problem Statement

Sample input

Analysis

approach 1: brute force, two full loops to iterate the array, for each current index, calculate the product except the current index value

code. time complexity: O(N^2).  space complexity: O(N)

def arrayOfProducts(array):
# Write your code here.
product = [1 for i in range(len(array))]
for i in range(len(array)):
res = 1
for j in range(len(array)):
if i != j:
res *= array[j]
product[i] = res
return product

approach 2: calculate the leftProducts and the rightProducts seperately, then multiple the right and left element of the current index

code.  time complexity: O(N).  space complexity: O(N)

def arrayOfProducts(array):
# Write your code here.
left = [1 for i in range(len(array))]
right = [1 for i in range(len(array))]
res = [1 for i in range(len(array))] leftProduct = 1
rightProduct = 1
for i in range(len(array)):
left[i] = leftProduct
leftProduct *= array[i] for i in reversed(range(len(array))):
right[i] = rightProduct
rightProduct *= array[i] for i in range(len(array)):
res[i] = right[i] * left[i]
return res

approach 3: avoid store the extra leftProducts and rightProducts, only update the products array

code.  time complexity: O(N).  space complexity: O(N)

def arrayOfProducts(array):
# Write your code here.
product = [1 for i in range(len(array))] leftProduct = 1
rightProduct = 1 for i in range(len(array)):
product[i] = leftProduct
leftProduct *= array[i] for i in reversed(range(len(array))):
product[i] *= rightProduct
rightProduct *= array[i] return product

Array of products的更多相关文章

  1. Magento-找出没有图片的产品

    最近维护网站,发现网站的产品很多都没有图片显示,看了一下是因为没有在后台勾选图片,就是 image small_image  thumbnail 这三项,就算有图片如果没有勾选的话也不会显示出来,产品 ...

  2. mongogogog

    $cmp,$eq,$gt,$gte,$lt,$lte,$ne$setEquals,$setIntersection,$setUnion,$setDifference,$setLsSubset,$any ...

  3. magento app开发遇到的问题及解决

    今天一直在解决Magento的APP接口调用数据异常的问题,调用/api/rest/category/:id 这个接口的时候,返回的所有目录的数据是一样的,原始代码是这样的. 1)请求地址 /api/ ...

  4. Pro ASP.NET MVC –第四章 语言特性精华

    C#语言有很多特性,并不是所有的程序员都了解本书我们将会使用的C#语言特性.因此,在本章,我们将了解一下作为一个好的MVC程序员需要了解C#语言的特性. 每个特性我们都只是简要介绍.如果你想深入了解L ...

  5. phalcon几种分页方法

    phalcon几种分页方法 一: use Phalcon\Paginator\Adapter\Model as PaginatorModel; // Current page to show // I ...

  6. phalcon(费尔康)框架学习笔记

    phalcon(费尔康)框架学习笔记 http://www.qixing318.com/article/phalcon-framework-to-study-notes.html 目录结构   pha ...

  7. 【ASP.NET Web API教程】1.1 第一个ASP.NET Web API

    Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson ...

  8. magento 常用方法集锦

    1,获得store的配置变量 Mage::getStoreConfig('sectionname/groupname/fields'); 1 Mage::getStoreConfig('section ...

  9. magento中的一些技巧

    1.加载某个attribute: $attributeCode=Mage::getModel('catalog/resource_eav_attribute')                     ...

  10. .net mvc笔记2_Essential C# Features

    Essential C# Features 1.Using Automatically Implemented Properties public class Product { private st ...

随机推荐

  1. windows下MinGW64编译环境设置

    windows下MinGW64编译环境设置 1. MinGW 介绍 MinGW 的全称是:Minimalist GNU on Windows .是将经典的开源 C语言 编译器 GCC 移植到了 Win ...

  2. UVM reg model 常见问题记录

    1.仿真log中报出大量的"include_coverage not located, did you mean ***"? (1) user在构建register model或者 ...

  3. zookeeper(1)-集群的搭建

    集群搭建 1. 下载二进制文件 $ wget --no-check-certificate https://mirrors.ustc.edu.cn/apache/zookeeper/zookeeper ...

  4. wake on lan magic packet

    局域网模式,必须电脑和手机在同一个 局域网,或者是网段内 外网模式  这个就是,你拿着手机,在任何地方,都可以开机电脑.

  5. Jupyter Notebook安装代码提示功能

    默认Jupyter Notebook没有安装代码提示功能,但是我们可以可通过如下命令安装和配置使得Jupyter Notebook具备代码提供功能. (确保Anaconda在环境变量里)1.电脑上搜索 ...

  6. kafka监控系统kafka eagle安装和使用

    一.kafka eagle介绍 kafka eagle 是一款由国内公司开源的Kafka集群监控系统,可以用来监视kafka集群的broker状态.Topic信息.IO.内存.consumer线程.偏 ...

  7. 常见的hash数据结构

    遍历 hash表是一种比较简单和直观的数据结构,在查找时也有很好的性能.但是hash表不能提供有序遍历,这个是其特性决定,所以不足为奇.但是,更为实际的一个问题是如果遍历整个hash表中的所有元素? ...

  8. vs2019 文件读取操作

    1 #include<stdio.h> 2 #define INF 10000000 3 int main() 4 { 5 FILE* fin , * fout ; 6 errno_t a ...

  9. abc289g题解

    考虑枚举卖出的物品个数\(i\),把\(b_i\)从大到小排序. 题目的某人会买物品的条件转化为\(b_i\geq p_j-c_j\),这说明卖出的物品的集合是排序后\(b\)的一段前缀,且卖出\(i ...

  10. 前端电商 sku 的全排列算法

    需求 需求描述起来很简单,有这样三个数组: let names = ["iPhone",'iPhone xs'] let colors = ['黑色','白色'] let stor ...