Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].

For example [1,2,3,4,5]:

We can divide into Right and Left array two parts. For Left[0] and Right[n-1], we set to 1;
 
We can get the product results like the image above.
Now the only thing we need to do is Left * Right. 
 
The way Left calculated is:
Left[i] = Left{i-1] * arr[i-1], i start from 1, i++
 
The way Right calculated is:
Right[j] = Right[j+1] * arr[j+1], j start from n-2, j--
 
function productArr(arr) {
let left = [];
let right = [];
let prod = [];
left[0] = 1;
right[arr.length - 1] = 1; for (let i = 1; i <= arr.length -1; i++) {
left[i] = left[i-1] * arr[i-1];
} for (let j = arr.length - 2; j >=0; j--) {
right[j] = right[j+1] * arr[j+1];
} prod = left.map((l, i) => l * right[i]); return prod
} console.log(productArr([1, 2, 3, 4, 5])); // [120, 60, 40, 30, 24]

[Algorithm] Array production problem的更多相关文章

  1. [Codeforces 863D]Yet Another Array Queries Problem

    Description You are given an array a of size n, and q queries to it. There are queries of two types: ...

  2. Yet Another Array Queries Problem CodeForces - 863D (暴力/思维)

    You are given an array a of size n, and q queries to it. There are queries of two types: 1 li ri — p ...

  3. [Algorithm] Tower Hopper Problem

    By given an array of number, each number indicate the number of step you can move to next index: For ...

  4. 863D - Yet Another Array Queries Problem(思维)

    原题连接:http://codeforces.com/problemset/problem/863/D 题意:对a数列有两种操作: 1 l r ,[l, r] 区间的数字滚动,即a[i+1]=a[i] ...

  5. [Algorithm] Max Chars Problem

    // --- Directions // Given a string, return the character that is most // commonly used in the strin ...

  6. Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  7. Co-variant array conversion from x to y may cause run-time exception

    http://stackoverflow.com/questions/8704332/co-variant-array-conversion-from-x-to-y-may-cause-run-tim ...

  8. Leetcode 之 Kth Largest Element in an Array

    636.Kth Largest Element in an Array 1.Problem Find the kth largest element in an unsorted array. Not ...

  9. 【LEETCODE】42、922. Sort Array By Parity II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

随机推荐

  1. JAVAEE学习——hibernate02:实体规则、对象状态、缓存、事务、批量查询和实现客户列表显示

    一.hibernate中的实体规则 实体类创建的注意事项 1.持久化类提供无参数构造 2.成员变量私有,提供共有get/set方法访问.需提供属性 3.持久化类中的属性,应尽量使用包装类型 4.持久化 ...

  2. 使用JDBC连接数据库的一些BUG

    题记:前几天用JDBC连接MYSQL数据库的时候,出现了一些BUG,有代码层次的,也有设置层次的, 下面的解决方法时我目前所遇到的,后期如果还有遇到的会进行补充. 一.出现:远程mysql_java. ...

  3. SRPG Studio 教程(一) 创建游戏及引用素材

    儿时玩红白机的时候,火纹和机器人大战这类战棋类的游戏就是博主的最爱,恰逢最近steam上上架了一款SRPG Studio用于制作火纹,趁这个机会学习一下,顺便记录下来. 秉承着一个程序猿的自我修养,以 ...

  4. FastReport.Net使用:[23]图表(Chart)控件

    图表基本设置 1.拖放一个图表控件到报表设计界面中. 2.右键菜单“编辑”或者双击图表进入图表编辑器 3.将原有的簇状柱状图删除,添加圆环图 4.绑定数据源,并且指定X,Y轴数据. X轴数据为科目名称 ...

  5. 【BZOJ 1923】1923: [Sdoi2010]外星千足虫 (高斯消元异或 | BITSET用法)

    1923: [Sdoi2010]外星千足虫 Description Input 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用“点足机”的统计结果.每行 包含一个 ...

  6. BZOJ1064 NOI2008假面舞会

    挺神的这题,发现只有环和链两种情况 搜索时我们只考虑环的,因为链可以看成找不到分类的环. 当成链时大小是的最大值是各链长的和,最小值是3 当成环时最大值是各环长的gcd,最小值是大于3的最小的ans的 ...

  7. NOI1999 JZYZOJ1289 棋盘分割 dp 方差的数学结论

    http://172.20.6.3/Problem_Show.asp?id=1289 除了下标一坨一坨屎一样挺恶心其他都还挺容易的dp,这道题才发现scanf保留小数位是四舍五入的,惊了. f[k][ ...

  8. [AtCoder-ARC073F]Many Moves

    题目大意: 有一排n个格子和2枚硬币. 现在有q次任务,每一次要你把其中一枚硬币移到x的位置上,移动1格的代价是1. 两枚硬币不能同时移动,任务必须按次序完成. 现在告诉你两枚硬币初始状态所在的位置a ...

  9. Linux使用C语言链接MsSQL

    1.安装gcc编译器 yum install gcc 2.下载freetds wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched ...

  10. Java实现-每天三道剑指Offre(2-4)

    实现一个单例模式 /** * 面试题2:实现单例模式 * * @author qiuyong 饿汉式 */ public class Singleton01 { private Singleton01 ...