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插入数据是出现Incorrect string value错误

    在向Mysql中添加中文数据时,报错(incorrect string value ) 字符转换不正确 插入语句:mysql>  insert into user1 (name,password ...

  2. Office常见问题及解决方法

    1.OneNote粘贴的图片比较模糊 问题原因:Microsoft为了使OneNote排版美观,在粘贴一些尺寸较大的图片时,会将图片进行缩放. 解决方法:右键->还原为原始尺寸

  3. PCA降维练习

    [题目] 1.现有我国大陆30个省.直辖市.自治区的经济发展状况数据集如表所示,包括8项经济指标:国民生产总值(A1):居民消费水平(A2):固定资产投资(A3):职工平均工资(A4):货物周转量(A ...

  4. Xamarin.Android 踩坑记

    将数据发送给微信 var dbFile = Path.Combine(DBSetting.GetSetting().DBDirectory, $"{BLL.SelectProject.DBN ...

  5. Django中关于Manager的使用

    首先介绍一下 manager 的使用场景,比如我们有一些表级别的,需要重复使用的功能,都可以使用 manager 来实现. 比如我们在前面的笔记中介绍的 model 的 create().update ...

  6. Consul 服务注册与发现

    Consul是一种分布式,高度可用且具有数据中心感知能力的解决方案,用于跨动态,分布式基础架构连接和配置应用程序. 1.下载consul.exe 2.在consul.exe文件夹地址栏输入cmd 3. ...

  7. 自定义函数式@FunctionalInterface异常接口

    1.添加注解 /** * 抛出异常函数接口 * * @author liunancun * @date 2021/2/5 */ @FunctionalInterface public interfac ...

  8. String.prototype.replace--替换字符串

    str.replace(regexp|substr, newSubStr|function)     API本身不改变原本的字符串,只是返回新的字符串例子:用函数作为第二个参数function rep ...

  9. 打卡node day07--图片上传,身份验证

    multer 模块  github:https://github.com/fanlina157/nodePro npm i multer fileRouter.js var express = req ...

  10. java中对list集合进行分组

    java中对list集合进行分组 Map<String, List<HealthImport>> excelIdCardNull = importList.stream() . ...