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 ...
随机推荐
- os-内核通知链notifier.c
8. linux内核通知链 8.1. 概述 在Linux内核中,各个子系统之间有很强的相互关系,某些子系统可能对其它子系统产生的事件感兴趣.为了让某个子系统在发生某个事件时通知感兴趣的子系统,Linu ...
- linux查找服务位置
- ORCAD中,怎么一次性去掉所有元器件下面的下划线呢
选择元器件,右键找到unset单击就可以去掉了
- pytorch的inverse算子转onnx失败
https://github.com/microsoft/onnxruntime-extensions/blob/main/tutorials/pytorch_custom_ops_tutorial. ...
- apt-get update 报错 Repository ' InRelease' changed its 'Suite' value from 'stable' to 'oldstable'
问题截图: 解决方案: apt-get update --allow-releaseinfo-change
- clickhouse 重启,软连接失效,增加存储路径
1. 启动停止命令 systemctl start clickhouse-server systemctl stop clickhouse-server systemctl status clickh ...
- 使用emplace_back的new initializer expression list treated as compound expression提示看聚合初始化和parameter pack
测试代码 使用emplace_back可以避免不必要的构造和拷贝,而是直接在向量的内存位置执行construct进行构造,代码看起来也更加简洁. 但是在使用的时候,会发现有一些和直观不太对应的情况.例 ...
- vw与百分比%的区别
单位, vw:只和设备宽度有关系 %:有继承关系
- SQL预编译
1.数据库预编译起源 (1)数据库SQL语句编译特性: 数据库接受到sql语句之后,需要词法和语义解析,优化sql语句,制定执行计划.这需要花费一些时间.但是很多情况,我们的一条sql语句可能会反复执 ...
- vite vue插件打包配置
import { defineConfig, UserConfigExport, ConfigEnv } from "vite"; import externalGlobals f ...