[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 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]

原文地址:https://www.cnblogs.com/Answer1215/p/10474421.html

时间: 2024-11-11 12:57:41

[Algorithm] Array production problem的相关文章

Berlekamp-Massey Algorithm [for Team Problem 5525]

Input: 第一行为两个正整数n,m 第二行为n个整数a1..an 最后一行为一个正整数k Output: 为一个整数,代表方案数对1000000007取模的值 Sample Input 5 3 1 1 2 0 2 2 Sample Output 3 来自毛爷爷17年论文 Berlekamp-Massey Algorithm直接开算 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const

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

原题连接:http://codeforces.com/problemset/problem/863/D 题意:对a数列有两种操作: 1 l r ,[l, r] 区间的数字滚动,即a[i+1]=a[i], a[l]=a[r] 2 l r ,[l, r] 区间的数字位置反转. 若干个操作之后输出a[b[i]]. 思路: 由于是在操作结束后输出,且b[i]的个数不多(<=100),所以可以通过反推求出答案. AC代码: 1 #include<iostream> 2 #include<cs

[Algorithm] Max Chars Problem

// --- Directions // Given a string, return the character that is most // commonly used in the string. // --- Examples // maxChar("abcccccccd") === "c" // maxChar("apple 1231111") === "1" function maxChar(str) { let

Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency_Pseudocode

This pseudocode from the book: <<Introduction to the Design and Analysis of Algorithms_Second Edition>> _ Anany LevitinNote that throughout the paper, we assume that inputs to algorithms fall within their specified ranges and hence require no

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-time-exception What it means is this Control[] controls = new LinkLabel[10]; // compile time legal controls[0] = new TextBox(); // compile time legal, ru

Algorithm Part I:Programming Assignment(3)

问题描述: Programming Assignment 3: Pattern Recognition Write a program to recognize line patterns in a given set of points. Computer vision involves analyzing patterns in visual images and reconstructing the real-world objects that produced them. The pr

【LEETCODE】39、第561题 Array Partition I

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: ArrayPairSum * @Author: xiaof * @Description: 561. Array Partition I * Given an array of 2n integers, your task is to group these integers

【LEETCODE】41、905. Sort Array By Parity

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: SortArrayByParity * @Author: xiaof * @Description: 905. Sort Array By Parity * Given an array A of non-negative integers, return an array

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

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: SortArrayByParityII * @Author: xiaof * @Description: 922. Sort Array By Parity II * Given an array A of non-negative integers, half of the