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. MYSQL-数据操作DDL,DML,DCL,DQL

    前言:MYSQL数据操作语言分为四种 1.DDL(数据定义语言):用来创建数据库中的表.索引.视图.存储过程.触发器等. 2.DML(数据操作语言):用来对表内数据的添加.更新.删除等. 3.DCL( ...

  2. [CSP-S2019] Emiya 家今天的饭

    洛咕 题意:原题面见链接,简单来说就是给出一个\(n*m\)的矩阵,每一行代表同一种烹饪方法,每一列代表同一种食材,\(a_{i,j}\)表示使用第i种烹饪方法第j种食材能做出多少种菜,要求至少做一道 ...

  3. GitBook的使用备忘

    GitBook环境搭建 npm install -g gitbook-cli # 新建目录,如helloworld cd helloworld # 执行此语句,需等待一段时间 gitbook init ...

  4. 镜像问题、简易安装,系统语言更改,中文输入,提高下载速度,Firefox的语言更改,Firefox上网速度慢

    一开始用的镜像不记得是从哪里下载过来的,反正 装好之后分辨率特别低.镜像放在: "D:\迅雷下载\2020大创\Ubuntu\ubuntu-16.04.6-desktop-i386.iso& ...

  5. Linux profile、bashrc、bash_profile

    一.profile 文件 1.profile 文件的作用 profile(/etc/profile),用于设置系统级的环境变量和启动程序,在这个文件下配置会对所有用户生效.当用户登录(login)时, ...

  6. surfaceview+mediaplayer

    public class VideosurfaceView extends SurfaceView implements SurfaceHolder.Callback, MediaPlayer.OnP ...

  7. c语言学习---gets()读取字符串,以及\0,fgets()put()fputs()

    #include<stdio.h> //gets()读取字符串, 可以读取空格 int main() { char num[2] = "";//gets 也会造成内存污 ...

  8. Echarts中国地图下钻

    //各省份的地图json文件 var provinces = { '上海': '/asset/get/s/data-1482909900836-H1BC_1WHg.json', '河北': '/ass ...

  9. SpringBoot2.2.2+SpringCloud-Hoxton.SR1整合eureka/gateway

    1.最近在学习SpringCloud分布式项目的知识,所以打算把自己学习到的知识也记录下来,为什么选择学习SpringCloud呢?因为分布式框架还有dubbo,如下图应该可以成为我为什么想学习Spr ...

  10. js 表格分页,ajax请求后台数据前台分页

    $(function(){ var url="后台给的地址"; var shuju=document.getElementById("shuju"); cons ...