Product of integers
On-Site Question 2 - SOLUTION
Problem
Given a list of integers, write a function that will return a list, in which for each index the element will be the product of all the integers except for the element at that index
For example, an input of [1,2,3,4] would return [24,12,8,6] by performing [2×3×4,1×3×4,1×2×4,1×2×3]
Requirements
You can not use division in your answer! Meaning you can't simply multiply all the numbers and then divide by eahc element for each index!
Try to do this on a white board or with paper/pencil.
Solution
If you look at the list above with the multiplication you'll notice we are repeating multiplications, such as 2 times 3 or 3 times 4 for multiple entries in the new list.
We'll want to take a greedy approach and keep track of these results for later re-use at other indices. We'll also need to think about what if a number is zero!
In order to find the products of all the integers (except for the integer at that index) we will actually go through our list twice in a greedy fashion.
On the first pass we will get the products of all the integers before each index, and then on the second pass we will go backwards to get the products of all the integers after each index.
Then we just need to multiply all the products before and after each index in order to get the final product answer!
Let's see this in action:
def index_prod(lst): # Create an empty output list
output = [None] * len(lst) # Set initial product and index for greedy run forward
product = 1
i = 0 while i < len(lst): # Set index as cumulative product
output[i] = product # Cumulative product
product *= lst[i] # Move forward
i +=1 # Now for our Greedy run Backwards
product = 1 # Start index at last (taking into account index 0)
i = len(lst) - 1 # Until the beginning of the list
while i >=0: # Same operations as before, just backwards
output[i] *= product
product *= lst[i]
i -= 1 return output
index_prod([1,2,3,4])
[24, 12, 8, 6]
index_prod([0,1,2,3,4])
[24, 0, 0, 0, 0]
Review the solution and make sure you understand it! It uses O(n) time and O(n) space complexity!
Good Job!
Product of integers的更多相关文章
- hdu 6125 -- Free from square(状态压缩+分组背包)
题目链接 Problem Description There is a set including all positive integers that are not more then n. Ha ...
- Codeforces Round #447 (Div. 2) 题解 【ABCDE】
BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...
- codeforces 894B - Ralph And His Magic Field - [数学题]
题目链接:https://cn.vjudge.net/problem/CodeForces-894B Ralph has a magic field which is divided into n × ...
- Codeforces 894.B Ralph And His Magic Field
B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field【数论/组合数学】
B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...
- HDU 6125 Free from square 状态压缩DP + 分组背包
Free from square Problem Description There is a set including all positive integers that are not mor ...
- Codeforces Round #232 (Div. 2) C
C. On Number of Decompositions into Multipliers time limit per test 1 second memory limit per test 2 ...
- Codeforces G. Nick and Array(贪心)
题目描述: Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday fro ...
- Codeforces Round #569 (Div. 2) B. Nick and Array
链接: https://codeforces.com/contest/1180/problem/B 题意: Nick had received an awesome array of integers ...
随机推荐
- linux7系统开机报错failed to start login service
1.开机报错failed to start login service 参考网站:https://unix.stackexchange.com/questions/264994/kali-sudden ...
- idea 安装 破解方法
参考:https://blog.csdn.net/qq_27686779/article/details/78870816 (1)下载破解补丁 把下载的破解补丁放在你的idea的安装目录下的bin的目 ...
- HTML5 Canvas ( 图形变换矩阵 ) transform, setTransform
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- IOS HTTP访问端口
Project dyld_sim raised exception class ENetHTTPClientException with message 'Error -1022 accessing ...
- clientdataset 用法
http://www.360doc.com/content/10/0709/01/2071424_37769962.shtml
- 为什么要在linux命令前加上 ./
为什么要在linux命令前加上 ./ ? 简述 执行unix或linux中除了path系统变量外的目录下的命令都要加./. 修改用户的 .bash_profile,在 PATH一行最后加上 “:.” ...
- 抽象类(abstract class)
package com.bjsxt.oop.abstractClass; //抽象类 public abstract class Animal { //因为父类的方法总是被重写 那就没写的必要了 但是 ...
- Android中decode JPG时建议使用inPreferQualityOverSpeed
在BitmapFactory.decodeBitmap方法中,参数BitmapFactory.Options里有一项是inPreferQualityOverSpeed:设为true的话,画质更好,加载 ...
- scala spark 调用hivecontext
import org.apache.spark.rdd.RDD def save(data: RDD[ModelReplay], modelKey: String, dt: String): Unit ...
- linux kernel 的配置及编译
1. 执行make menuconfig 配置内核 2. 执行make zImage 编译内核 3. 执行make modules 编译模块 4. 内核源代码的配置及编译系统 Makefile Kco ...