Array of products
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的更多相关文章
- Magento-找出没有图片的产品
最近维护网站,发现网站的产品很多都没有图片显示,看了一下是因为没有在后台勾选图片,就是 image small_image thumbnail 这三项,就算有图片如果没有勾选的话也不会显示出来,产品 ...
- mongogogog
$cmp,$eq,$gt,$gte,$lt,$lte,$ne$setEquals,$setIntersection,$setUnion,$setDifference,$setLsSubset,$any ...
- magento app开发遇到的问题及解决
今天一直在解决Magento的APP接口调用数据异常的问题,调用/api/rest/category/:id 这个接口的时候,返回的所有目录的数据是一样的,原始代码是这样的. 1)请求地址 /api/ ...
- Pro ASP.NET MVC –第四章 语言特性精华
C#语言有很多特性,并不是所有的程序员都了解本书我们将会使用的C#语言特性.因此,在本章,我们将了解一下作为一个好的MVC程序员需要了解C#语言的特性. 每个特性我们都只是简要介绍.如果你想深入了解L ...
- phalcon几种分页方法
phalcon几种分页方法 一: use Phalcon\Paginator\Adapter\Model as PaginatorModel; // Current page to show // I ...
- phalcon(费尔康)框架学习笔记
phalcon(费尔康)框架学习笔记 http://www.qixing318.com/article/phalcon-framework-to-study-notes.html 目录结构 pha ...
- 【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 ...
- magento 常用方法集锦
1,获得store的配置变量 Mage::getStoreConfig('sectionname/groupname/fields'); 1 Mage::getStoreConfig('section ...
- magento中的一些技巧
1.加载某个attribute: $attributeCode=Mage::getModel('catalog/resource_eav_attribute') ...
- .net mvc笔记2_Essential C# Features
Essential C# Features 1.Using Automatically Implemented Properties public class Product { private st ...
随机推荐
- 1.mysql创建索引
-- 创建一个普通索引(方式①)create index 索引名 ON 表名 (列名(索引键长度) [ASC|DESC]);-- 创建一个普通索引(方式②)alter table 表名 add ind ...
- RabbitMQ 修改密码
C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.14\sbin> rabbitmqctl.bat change_password adm ...
- 关闭win10“快速启动”功能
windows正常运行时间的统计问题 windows任务管理器中,在性能选项卡中可以查看系统的运行时间(开机时间) 正常情况下,如果电脑关机,那么再次开机后,该时间会被重置 但是,如果电脑处于上面所说 ...
- 日志服务器 nagios log server
官网 https://www.nagios.com/products/ 可官网下载手动安装也可命令安装 命令安装 cd /tmp wget https://assets.nagios.com/down ...
- charls抓包的乱码问题解决
1. 在charls的安装目录下,去修改配置文件的值 ----Charles.ini,内容如下 2. SSL proxy setting 设置如下图 3. 顺便说一下,charls抓取 https的包 ...
- tif文件拼接+转换成netcdf格式
2022-11-16 11:33 手上有若干个SRTM1V3.0 的高程数据,为HGT格式,需要将其拼接并转换成netcdf格式,以让ncl能够进行读取 步骤: gdal_translate A.hg ...
- pandas通过sqlalchemy写入pgsql报错can't adapt type 'numpy.int64'
其实以前也遇到过,后来不了了之,但今天又出现了,还是大概记录下. 我个人习惯按我自己的理解搞事情,只要结果对就行,但不一定对. 分析下原因,突然想到dataframe中有一列全是列表,列表中全是整数, ...
- [*]Is L2 Physics-Informed Loss Always Suitable for Training Physics-Informed Neural Network?
NeurIPS 2022 本篇工作对PINN中的物理损失进行了探究,作者认为L2损失并不总是适用于训练PINN.并从PDE解的稳定性角度给出了理论性的说明.读了这篇文章,感觉自己的毕业论文做的十分浅显 ...
- JAVA仓库管理系统(附源码+调试)
JAVA仓库管理系统--三只松鼠仓库管理系统功能描述(1)登录模块:登录信息等存储在数据库中(2)基本档案管理模块:供货商管理,销售商管理,货品档案管理,仓库管理(3)采购订货模块:用户可以通过查询条 ...
- C++ primer笔记 -标准库类型
最重要的两个标准库类型:string和vector string 类型的输入操作符: 1.读取并忽略开头所有的空白符 2.读取字符直至再次遇到空白字符,读取终止 string对象的基本操作: stri ...