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 ...
随机推荐
- undefined reference to symbol xxxxx和undefined symbol:xxxx错误的原因分析以及解决方法
Linux下编译程序时,经常会遇到"undefined reference to XXX" 报错,或者运行时出现undefined symbol:xxxx报错. 这里总结一些可能的 ...
- vue2的响应式原理
响应式的话,主要指的是这个状态改变以后,视图要去主动更新 这个过程,vue是通过两个步骤来实现的 1 数据的劫持 数据劫持也叫做数据拦截,通过Object.defineProperty来把对象中的每一 ...
- SqlServer 优化的技巧
1.避免使用 select * select * 不会走覆盖索引,会出现大量的回表操作,从而导致SQL的查询性能很低 2.用union all 代替 union 1.使用union后,可以获取排重复后 ...
- js实现点击按钮或div显示与隐藏div
var box = document.getElementById("box"); var btn = document.getElementById("btn" ...
- maven加载本地的jar包
方式1 ,通过scope = system的方式加载 <dependency> <groupId>com.sun.jna</groupId> <artifac ...
- vue super flow 多种形状
1 <template> 2 <v-container class="workflow-container" grid-list-xl fluid> 3 & ...
- 华三防火墙主备ACL
dis cur | in policy 查看 policy-based-route 有线网络 permit node 5 policy-based-route 有线网络 permit node 10 ...
- 三种方式实现RPC调用
一:RabbitMQ实现RPC调用 客户端: import pika import uuid class FibonacciRpcClient(object): def __init__(self): ...
- 光纤加速计算 383-高速信号处理板 XCKU060的双路QSFP+光纤PCIe 卡 XCKU060板卡
基于kintex UltraScale XCKU060的双路QSFP+光纤PCIe 卡 一.板卡概述 本板卡系北京太速科技自主研发,基于Xilinx UltraScale Kintex系列FPGA ...
- VUE2.0 脚手架搭建项目,如何配置本地IP地址访问项目,详解
1.首先找到config文件夹目录下的 index.js文件 // Various Dev Server settings //host: 'localhost' //将localhost进行替换成 ...