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的更多相关文章

  1. Magento-找出没有图片的产品

    最近维护网站,发现网站的产品很多都没有图片显示,看了一下是因为没有在后台勾选图片,就是 image small_image  thumbnail 这三项,就算有图片如果没有勾选的话也不会显示出来,产品 ...

  2. mongogogog

    $cmp,$eq,$gt,$gte,$lt,$lte,$ne$setEquals,$setIntersection,$setUnion,$setDifference,$setLsSubset,$any ...

  3. magento app开发遇到的问题及解决

    今天一直在解决Magento的APP接口调用数据异常的问题,调用/api/rest/category/:id 这个接口的时候,返回的所有目录的数据是一样的,原始代码是这样的. 1)请求地址 /api/ ...

  4. Pro ASP.NET MVC –第四章 语言特性精华

    C#语言有很多特性,并不是所有的程序员都了解本书我们将会使用的C#语言特性.因此,在本章,我们将了解一下作为一个好的MVC程序员需要了解C#语言的特性. 每个特性我们都只是简要介绍.如果你想深入了解L ...

  5. phalcon几种分页方法

    phalcon几种分页方法 一: use Phalcon\Paginator\Adapter\Model as PaginatorModel; // Current page to show // I ...

  6. phalcon(费尔康)框架学习笔记

    phalcon(费尔康)框架学习笔记 http://www.qixing318.com/article/phalcon-framework-to-study-notes.html 目录结构   pha ...

  7. 【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 ...

  8. magento 常用方法集锦

    1,获得store的配置变量 Mage::getStoreConfig('sectionname/groupname/fields'); 1 Mage::getStoreConfig('section ...

  9. magento中的一些技巧

    1.加载某个attribute: $attributeCode=Mage::getModel('catalog/resource_eav_attribute')                     ...

  10. .net mvc笔记2_Essential C# Features

    Essential C# Features 1.Using Automatically Implemented Properties public class Product { private st ...

随机推荐

  1. pillow 创建图片并添加一些自定义信息

    from PIL import Image vm = Image.new('RGBA', (dshape[1], dshape[0])) vm = Image.fromarray(np.array(s ...

  2. SAP SD VA01 销售订单中的自动价格更新

    场景 :当用户使用假定物料" A"创建销售订单时,确定了价格,但随后用户意识到需要更改物料,因此他们更改了订单中的物料. 现在,它显示价格已经重新确定,但是在项目条件页面中时,他们 ...

  3. kali上的apache2

    之前总是疑惑为什么kali上的apache服务称之为apache2,但是也没想到去找找答案,今天突然想到了,简单搜索了一下大致就是, 现在Apache HTTP 存在三种版本, 1.3 2.0 和2. ...

  4. 兼容ie8的Html+Css+Js

    1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset=" ...

  5. Codeforces Round #843 (Div. 2) C【思维】

    https://codeforces.com/contest/1775/problem/C 题意 题意是说,给你n和x,你要求出最小的满足要求的m,使得 \(n\)&\((n+1)\)& ...

  6. Unity实现手机录音功能

    using UnityEngine;using System.Collections;public class MicPhoneScripts : MonoBehaviour{    private ...

  7. c++基础: uint8_t uint16_t uint32_t uint64_t size_t ssize_t数据类型

    https://blog.csdn.net/lzx_bupt/article/details/7066577 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个 ...

  8. ES相关问题

    取消写入保护 参考:https://blog.csdn.net/xudailong_blog/article/details/80850159

  9. android kotlin data class的公共参数解决

    kotlin的 data class 不能简略的继承 另一个 data class(要初始化对应父类的值) 尝试使用 sealed class 解决 sealed class CommonParams ...

  10. 发现C++程序中未释放的内存空间

    本篇先后介绍在windows中使用visual studio定位未释放的内存.在linux中使用valgrind定位未释放的内存. Windows+Visual Studio 2015 (企业版) 准 ...