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 ...
随机推荐
- P5736 质数筛
原题连接 一看到这个熟悉的输入,我们就立马反应过来要请出一维数组来记录一下输入的数据.现在数据的存储解决了,紧接着来剖析一下步骤: 输入数据 一个一个的判断是否为质数 筛去合数 输出质数 理清了思路后 ...
- demo code
using System.Reflection; // 引用这个才能使用Missing字段 namespace hello{ public partial class Form1 : Form ...
- C# List间的交集并集差集
一.简单类型List的交集并集差集 1.先定义两个简单类型的List List<int> listA = new List<int>() { 1, 2, 3, 4, 5, 6, ...
- Mybatis插件-分批次插入数据
背景 有时候使用insert into xxx values (),()语句插入大量数据时,会使得SQL语句超长,为了解决这个问题,在Mybatis中编写一个分批次插入的插件. 实现 package ...
- 增加网络位置CMD脚本
创建.bat脚本 net use Z: \\192.168.X.X\Share /user:用户名 /persistent:YES 密码 persistent:YES是保存密码.下次开机也生效.
- 【Ubuntu】设置桌面文件夹路径
Ubuntu 系统会将桌面文件夹路径默认设置为 $HOME/Desktop,包括文档.下载.图片等文件夹路径都有各自的默认路径.若想更改这些文件夹路径,可参考『此链接』. 首先到希望更改的路径下建立桌 ...
- Iframe 默认高度、宽度
项目中还在使用 Iframe ,一次看代码时发现 Iframe 没有指定高度.宽度,可是在页面上显示的时候却有高度.宽度.想着应该是 Iframe 的默认值,于是写了一个简单的页面,代码如下: 1 & ...
- 服务器做bond,交换机做port-channel
端口绑定的种类(具体介绍网上很多参考) balance-rr or 0 active-backup or 1 balance-xor or 2 broadcast or 3 802.3ad or 4 ...
- 记下HTML中图片的路径
1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 < ...
- Vue非父子組件(爺孫關係)通信Provide&&Inject
通常,当我们需要从父组件向子组件传递数据时,我们使用 props.想象一下这样的结构:有一些深度嵌套的组件,而深层的子组件只需要父组件的部分内容.在这种情况下,如果仍然将 prop 沿着组件链逐级传递 ...