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 ...
随机推荐
- NLP—WordNet——词与词之间的最小距离
WordNet,是由Princeton 大学的心理学家,语言学家和计算机工程师联合设计的一种基于认知语言学的英语词典.它不是光把单词以字母顺序排列,而且按照单词的意义组成一个“单词的网络”.我们这次的 ...
- HTML5 ES6 语法基础
// 解构赋值 let [a, b, c, [s,e],d] = ["aa", "bb", "cc", [12, 23], "dd ...
- 8.tomcat认证访问
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 接上例:namespce的作用除了在前面提到的避免协同开发名字冲突外,还为认 ...
- jd-eclipse反编译插件的在线安装和使用
jd-eclipse反编译插件的在线安装和使用 JD-Eclipse是一个Eclipse平台的插件.它允许您调试所有的Java源代码,有了它,以后调试的时候ctrl键就可以一键到底啦.下面简单说说ec ...
- J2SE 8的编译
动态加载(修改)服务.高性动态业务逻辑实现(用脚本或模板引擎实现效率满足不了需求) package compile; import java.io.File; import java.io.IOExc ...
- java ee7 -- Java Bean验证
针对对象.对象成员.方法.构造函数的数据验证. 1. 一个验证的小例子 (1) 添加引用jar <dependency> <groupId>org.hibernate.vali ...
- Spring MVC 运行流程图
- 5.15 python 面向对象的软件开发&领域模型
1.面向对象的软件开发 参考地址::http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label14 面向对象的软件工程包括下面几个部: ...
- js执行机制(1)
1.参考执行结果 setTimeout(function () { console.log('执行定时任务'); }); new Promise(function (resolve) { consol ...
- int和Integer区别
Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本数据类型,但是为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper cl ...