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 ...
 
随机推荐
- sqlserver导入导出数据库结构及创建用户分配权限
			
1.创建用户分配权限 https://www.cnblogs.com/jennyjiang-00/p/5803140.html 2.sqlserver2008导出表结构和表数据 导出表结构 htt ...
 - iTunes 错误 -50
			
iTunes,给苹果安装软件,这个软件的体验这么差!!! 手机上基本打不开AppStore,用电脑iTunes,经常莫名其妙的错误代码冒出. 速度奇慢无比. error -50 打开iTunes -- ...
 - sql 数据库修复
			
数据库修复 exec sp_dboption 'dbname1','single user',‘true’ dbcc checkdb('dbname1') dbcc checkdb('dbname1' ...
 - 在spring引入log4j(非web项目)
			
https://blog.csdn.net/u012578322/article/details/78012183 在spring中使用log4j 引入log4j软件包 配置log4j属性 加载log ...
 - 相对固定位置 relative absolute
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
 - Mysql 触发器  A表记录到B表
			
1:查询出需要的列名 备用 #列名 select COLUMN_NAME from information_schema.columns where TABLE_SCHEMA='yunpiaobox_ ...
 - .NET MVC 两种视图引擎(Razor、Aspx)
			
ASPX 优点: 通过上面小小的对比,不难看出,与ASP.NET MVC紧密集成,对于以往ASP.NET开发人员有更好体验.其实它还有其他几优点: ●智能感应 ...
 - Cookie的过期时间设置
			
https://pan.baidu.com/s/1ibUQhLt6ZgVyhVM6mnrtHg 密码:9psc
 - with as (转)
			
sql with as 用法(适用sqlserver,好像oracle也适用) Server 2005中提供了公用表表达式(CTE),使用CTE,可以使SQL语句的可维护性,同时,CTE要比表变量的效 ...
 - mac安装protobuf2.4.1时报错./include/gtest/internal/gtest-port.h:428:10: fatal error: 'tr1/tuple' file not found和google/protobuf/message.cc:175:16: error: implicit instantiation of undefined template
			
通过网上下载的protobuf2.4.1的压缩文件,然后进行安装,./configure和make时遇到了两个问题. 正常的安装步骤如下: ./configure make make check m ...