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 ...
随机推荐
- HTML5 Canvas ( 线性渐变, 升级版的星空 ) fillStyle, createLinearGradient, addColorStop
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ASP.net显示当前系统在线人数
void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 Application.Lock(); if (Applic ...
- Ping ip能ping通,ping计算机名ping不通,网络共享不能访问
名称 协议 端口 NetBIOS Name Service UDP 137 NetBIOS Datagram Service UDP 138 NetBIOS Session Service TCP 1 ...
- eclipse插件常用网址链接
目录 jar下载地址 java maven svn erMaster linux镜像ISO: http://www.linuxfly.org/post/659/ virtual下载: ...
- protocol_link
蔡燧林:1992—2000年教育部考试中心研究生数学命题组组长现在退休养老.要想办法弄到他编的书(ps:别问怎么弄到,我和我同学都能弄到,你怎么会不能弄到呢)李林:目前在导航独家授课,他能屡屡命中考研 ...
- js实现jquery函数animate动画效果
<script> function animate(obj, json, interval, sp, fn) { clearInterval(obj.timer); function ge ...
- The requested resource (/) is not available解决办法
The requested resource (/) is not available HTTP Status 404(The requested resource is not available) ...
- 基元线程同步构造之信号量(Semaphore)
信号量(semaphore)不过是由内核维护的 int32变量而已,(说通俗点就是好比一个线程容器里面允许执行的线程数,0计数就是允许执行的0个线程数,1就是允许执行的1个线程数,2就是允许执行的2个 ...
- LOG4J spring与mybatis整合
1.导入包log4j-1.2.17.jar <dependency> <groupId>log4j</groupId> ...
- springboot重定向
参考https://www.cnblogs.com/kxkl123/p/7800967.html public String test() { return "redirect:/" ...