[Algorithm] Array production problem
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 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的更多相关文章
- [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: ...
- 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 ...
- [Algorithm] Tower Hopper Problem
By given an array of number, each number indicate the number of step you can move to next index: For ...
- 863D - Yet Another Array Queries Problem(思维)
原题连接:http://codeforces.com/problemset/problem/863/D 题意:对a数列有两种操作: 1 l r ,[l, r] 区间的数字滚动,即a[i+1]=a[i] ...
- [Algorithm] Max Chars Problem
// --- Directions // Given a string, return the character that is most // commonly used in the strin ...
- 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 ...
- 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 ...
- 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 ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- c++ getline()
#include <iostream>#include <string> int main (){ std::string name; std::cout << & ...
- poj2531(深搜剪枝)
题意就是把节点分成A.B两组,节点间距C给了,要求解分组的方法,使得∑Cij (i∈A,j∈B)最大. 首先把所有节点都放在一组,然后采用深度优先搜索的方法,对每一个节点都做判断是否应该移到另一组去, ...
- Bakery CodeForces - 707B (最短路的思路题)
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. The ...
- Cpp下的深拷贝与浅拷贝探究
下面,通过代码来说说C++中的深浅拷贝 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; cl ...
- Vue 2.0学习(六)内置指令
基本指令 1.v-cloak v-cloak不需要表达式,它会在Vue实例结束编译时从绑定的HTML元素上移除,经常和CSS的display:none配合使用. <div id="ap ...
- Scrapy学习篇(三)之创建项目
创建项目 创建项目是爬取内容的第一步,之前已经讲过,Scrapy通过scrapy startproject <project_name>命令来在当前目录下创建一个新的项目. 下面我们创建一 ...
- 网络流—最大流(Edmond-Karp算法)
一.含义 从源点到经过的所有路径的最终到达汇点的所有流量和 例如: 在这个图中求源点1,到汇点4的最大流.答案为50,其中1->2->4为20 :1->4为20 :1->2-& ...
- django之cookie、session和ajax
1 Cookie cookie是什么? 保存在客户端浏览器上的键值对 {k:v} cookie依附在请求头或响应头中出现 发送请求时,会自动携带自己网站的cookie 应用: 实现登录 投票 1. ...
- Cookie&Session会话技术
一.会话技术简介 1.存储客户端的状态 由一个问题引出今天的内容,例如网站的购物系统,用户将购买的商品信息存储到哪里?因为Http协议是无状态的,也就是说每个客户访问服务器端资源时,服务器并不知道该客 ...
- [转]Android Activity和Fragment的转场动画
Android Activity和Fragment的转场动画 Activity转场动画 Activity的转场动画是通过overridePendingTransition(int enterAnim, ...