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 ...
随机推荐
- oracle排查慢sql
查询最慢的SQL select * from (select sa.SQL_TEXT, sa.SQL_FULLTEXT, sa.EXECUTIONS "执行次数", round(s ...
- 【git】3.3 git分支-分支管理
资料来源 (1) https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%AE%A1%E7%90%86 ...
- sqoop mysql2hive
a./etc/profile添加export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:$HIVE_HOME/lib/*b.将hive-site.xml 拷贝到 $SQOO ...
- NVIDIA的GPU算力Compute Capalibity
可查看官方查询地址:https://developer.nvidia.com/cuda-gpus
- chklist
1. 重复检查的必要性.一段代码如果在测试期间没问题,也要间隔几天再去观察是否有问题.2. 如果是集群式的服务,使用定时任务要采用分布式锁,或使用工具随机发送一台都可以.3. 如果任务跑失败,需要支持 ...
- Linux工作中最常用命令整理
ls 命令:显示指定工作目录下之内容 ls -a # 显示所有文件夹,包含隐藏的. 和.. ls -l # 显示文件的详细信息,包含文件形态,权限,所属,大小,其实就是平常用的 ll ll -h # ...
- 34.MySQL 架构
一主两从 双机热备 原理:
- 假设业务需要,在页面一屏中一次性展示大量图片(100张),导致img组件同时发起大量的请求,导致浏览器性能被消耗殆尽,页面卡死。怎么优化?
1.所有图片先提供占位图2.懒加载思想3.使用队列的方式4.通过信号量的机制,结合队列.例如设置可用资源数为10,则保证同一时刻,只有10张图片正在请求,其它图片处于loading状态,任意图片请求完 ...
- 【C学习笔记】day1-3 判断1000年---2000年之间的闰年
#include<stdio.h> int main() { for (int y = 1000; y <= 2000; y++) if (y % 100 == 0) { if (y ...
- 光纤加速卡第410篇:基于XCVU9P+ C6678的40G光纤的加速卡 光纤的加速卡 无线通信
光纤加速卡第410篇:基于XCVU9P+ C6678的40G光纤的加速卡 光纤的加速卡 无线通信 基于XCVU9P+ C6678的40G光纤的加速卡 一.板卡概述 二.技术指标 • 板卡为自定义 ...