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 ...
随机推荐
- 传统三层架构与DDD分层架构
参考 https://www.cnblogs.com/sandyliu1999/p/4969445.html
- Activity服务类-4 HistoryService服务类
一共个方法15个方法 用于查询历史工作流信息1.创建查询(7个方法)//创建一个新的编程查询来搜索{@link HistoricProcessInstance}.HistoricProcessInst ...
- Hadoop 执行 hdfs 命令烦人的警告信息
问题描述: 安装完 hadoop,执行 hdfs dfs -ls 命令时,报警告如下: -- ::, WARN util.NativeCodeLoader: Unable to load native ...
- mysql 中文编码问题
- JAVA的String类的常用方法(转载)
Java-String类的常用方法总结 一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的f ...
- 关于sql 增删改
1.更改数据库的名称 --更改数据库的名称,逗号前面是之前的,后面是改成的名 sp_renamedb student,xuesheng 2.表中有数据的情况下再添加列.删除列 --后来添加列,只能默认 ...
- Python 列表表达式 ,迭代器(1)
python 环境 3.5 1.列表: s = []; for i in s: i = handleFunction(i); s.append(i) .列表 s=[handleFunction(i) ...
- Mysql 中json 相关函数的使用
1.JSON_LENGTH: select content from test1 ["1","2","3","4",&q ...
- RNA-seq 安装 fastaqc,tophat,cuffilnks,hisat2
------------------------------------------ 安装fastqc------------------------------------------------- ...
- python3 django连接mysql,同步表结构
第一步:安装PyMySQ代替MySQLdb pip3 install PyMySQL 然后在工程目录的__init__.py中填写下面两句话 import pymysql pymysql.inst ...